簡體   English   中英

PHP:如何檢查API中是否存在條目?

[英]PHP: How to check if an entry exists in API?

我正在查詢Wikipedia API。 通常,我得到以下內容,並且回顯出摘要。

array:4 [▼
"pageid" => 13275
"ns" => 0
"title" => "Hungary"
"extract" => """
<p><span></span></p>\n
<p><b>Hungary</b> (<span><span>/<span><span title="/ˈ/ primary stress follows">ˈ</span><span title="'h' in 'hi'">h</span><span title="/ʌ/ short 'u' in 'bud'">ʌ</span><span title="/ŋ/ 'ng' in 'sing'">ŋ</span><span title="'g' in 'guy'">ɡ</span><span title="/ər/ 'er' in 'finger'">ər</span><span title="/i/ 'y' in 'happy'">i</span></span>/</span></span>; Hungarian: <span lang="hu"><i>Magyarország</i></span> <span title="Representation in the International Phonetic Alphabet (IPA)">[ˈmɒɟɒrorsaːɡ]</span>) is a parliamentary constitutional republic in Central Europe. It is situated in the Carpathian Basin and is bordered by Slovakia to the north, Romania to the east, Serbia to the south, Croatia to the southwest, Slovenia to the west, Austria to the northwest, and Ukraine to the northeast. The country's capital and largest city is Budapest. Hungary is a member of the European Union, NATO, the OECD, the Visegrád Group, and the Schengen Area. The official language is Hungarian, which is the most widely spoken non-Indo-European language in Europe.</p>\n

但是,如果該條目在Wiki中不存在,那么我會明白這一點。

array:3 [▼
"ns" => 0
"title" => "Kisfelegyhaza"
"missing" => ""
]

所以我的問題是如何檢查提取物是否存在?

我嘗試了以下操作,但不起作用。

$wiki_array = The data received from Wiki
if (array_key_exists('extract',$wiki_array)){
 // do something
}
$wiki_array = The data received from Wiki
if( isset($wiki_array['extract']) ){
 // do something
}

isset($ var)檢查是否設置了var(所以不為null)

對於面臨相同問題的任何人,這是我使用的解決方案。

foreach($wiki_array['query']['pages'] as $page){
  if( isset($page['extract']) ){
    echo '<p>';
    echo $page['extract'];
    echo '</p>';
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM