is_array says array is not an array!
Posted: Tue Oct 12, 2010 8:37 pm
It is late, and I may be missing something very obvious here, but I am having trouble accessing the telephone values in some xml because when there is more than one number, simpleXml creates $xmlObj->TelephoneNumbers->TelephoneNumber as an array, but when there is only one, it creates it as SimpleXMLElement Object. If I try to use is_array() on $xmlObj->TelephoneNumbers->TelephoneNumber to determine whether I loop over it or not, the value I get back is false even though print_r() tells me it is an array, and if I actually use foreach on it, it behaves like an array. Can anyone tell me why is_array returns false?
My small testcase is below, followed by the results:
Produces:
TIA.
My small testcase is below, followed by the results:
Code: Select all
<?php
function xmlentities($string) {
return str_replace ( array ( '&', ' ', '"', "'", '<', '>' ), array ( '&', ' ' , '"', ''' , '<' , '>' ), $string );
}
function is_array_abled(&$x)
{
return (bool)($x instanceof ArrayAccess or is_array($x));
}
$xml = <<<EOD
<Applicants>
<TelephoneNumbers>
<TelephoneNumber>
<Number>01244123456</Number>
<Type>Notification</Type>
</TelephoneNumber>
<TelephoneNumber>
<Number>78787654321</Number>
<Type>Mobile</Type>
</TelephoneNumber>
</TelephoneNumbers>
</Applicants>
EOD;
printf("<h3>XML:</h3>");
print(nl2br(xmlentities($xml)));
$xmlObj = simplexml_load_string($xml);
printf("<h3>SimpleXML Object:</h3>");
print("<pre>");
print_r($xmlObj);
print("</pre>");
if(is_array($xmlObj->TelephoneNumbers->TelephoneNumber)) {
$result = 'true';
} else {
$result = 'false';
}
printf("is_array(\$xmlObj->TelephoneNumbers->TelephoneNumber) gives: %s<br><br>", $result);
// Try printing both sets of numbers
print("But using a foreach loop still works and we can print out the numbers...<br>");
foreach ($xmlObj->TelephoneNumbers->TelephoneNumber as $phoneObj) {
printf("Phone: %s, Number: %s<br>", $phoneObj->Type, $phoneObj->Number);
}
?>Code: Select all
XML:
<Applicants>
<TelephoneNumbers>
<TelephoneNumber>
<Number>01244123456</Number>
<Type>Notification</Type>
</TelephoneNumber>
<TelephoneNumber>
<Number>78787654321</Number>
<Type>Mobile</Type>
</TelephoneNumber>
</TelephoneNumbers>
</Applicants>
SimpleXML Object:
SimpleXMLElement Object
(
[TelephoneNumbers] => SimpleXMLElement Object
(
[TelephoneNumber] => Array
(
[0] => SimpleXMLElement Object
(
[Number] => 01244123456
[Type] => Notification
)
[1] => SimpleXMLElement Object
(
[Number] => 78787654321
[Type] => Mobile
)
)
)
)
is_array($xmlObj->TelephoneNumbers->TelephoneNumber) gives: false
But using a foreach loop still works and we can print out the numbers...
Phone: Notification, Number: 01244123456
Phone: Mobile, Number: 78787654321