Clinton Green Clinton Green - 1 year ago 117
PHP Question

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

I am querying the Wikipedia API. Normally I get the following, and I echo out the extract.

array:4 [▼
"pageid" => 13275
"ns" => 0
"title" => "Hungary"
"extract" => """
<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

But if the entry does not exist in Wiki then I get this.

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

So my question is how do I check if extract exists?

I tried the following but it does not work.

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

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

isset($var) to check if that var is setted (so not null)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download