Page 1 of 1

if statement parameter for an array

Posted: Tue Dec 22, 2009 5:08 pm
by JackD
The following code is working on our website:

$EAN_List = (array)$parsed_xml->Items->Item->ItemAttributes->EAN;
if (is_array($EAN_List) and $EAN_List[0])
{ foreach ($EAN_List as $i=>$Item_Data)
{ echo $Item_Data; }
}

What does the $EAN_List[0] parameter do? Why could I not just check to see if it is an array and not include the $EAN_List[0]?

Re: if statement parameter for an array

Posted: Tue Dec 22, 2009 6:16 pm
by cpetercarter

Code: Select all

 
if ($EAN_list[0])
 
is a way of writing

Code: Select all

 
if ($EAN_list[0] == true)
 
$EAN_list[0 ]will evaluate to true if it contains a non-zero value.So, the 'if' test in your code is asking:

is $EAN_list an array? And does the first element of the array contain a non-zero value?

Re: if statement parameter for an array

Posted: Tue Dec 22, 2009 6:41 pm
by JackD
Thank you very much!! :mrgreen: