2016-10-13 3 views
0

위키 백과 API를 쿼리하고 있습니다. 일반적으로 다음과 같은 결과를 얻었습니다.PHP : 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 

위키에 항목이 없으면이 항목이 표시됩니다. 추출물이있는 경우

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

그래서 제 질문은 어떻게 확인합니까입니까?

다음을 시도했지만 작동하지 않습니다. 그 var에 (널 그렇게되지 않음) 잘 살고 경우

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

답변

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

는 isset ($ var에)는 같은 문제에 직면하고있는 사람들을위한

+0

안녕하세요. 불행히도 작동하지 않습니다. 그것이 분명히 있더라도 결코 '추출물'을 찾지 않는 것 같습니다. –

+0

사실 그것은 방법에 관한 부분입니다, 나는 단지 $ wiki_array [ 'extract']보다 더 깊이 움직여야 작동 할 수 있습니다. 감사 –

0

확인, 여기 내가 사용하는 솔루션입니다.

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