PHP: 如何检查 API 中是否存在条目?
PHP: How to check if an entry exists in API?
我正在查询维基百科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" => ""
]
所以我的问题是如何检查 extract 是否存在?
我尝试了以下但它不起作用。
$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) 检查是否设置了该变量(因此不为空)
对于遇到相同问题的任何人,这是我使用的解决方案。
foreach($wiki_array['query']['pages'] as $page){
if( isset($page['extract']) ){
echo '<p>';
echo $page['extract'];
echo '</p>';
}
}
我正在查询维基百科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" => ""
]
所以我的问题是如何检查 extract 是否存在?
我尝试了以下但它不起作用。
$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) 检查是否设置了该变量(因此不为空)
对于遇到相同问题的任何人,这是我使用的解决方案。
foreach($wiki_array['query']['pages'] as $page){
if( isset($page['extract']) ){
echo '<p>';
echo $page['extract'];
echo '</p>';
}
}