SimpleXMLElement->数组,in_array不起作用

时间:2014-05-23 10:09:53

标签: php arrays simplexml

print_r($xml);

SimpleXMLElement Object
(
    [groupId] => Array
        (
            [0] => 1
            [1] => 5
        )
)

in_array(1, $xml->groupId)对此无效:PHP Warning: in_array() expects parameter 2 to be array, object given

print_r((array)$xml->groupId);仅打印数组中的第一个元素:

Array
(
    [0] => 1
)

我如何正确检查groupId中存在的元素,没有json_decode(json_encode($xml->groupId));这样的黑客?

XML print_r($xml->asXML());

<?xml version="1.0"?>
<return>
    <groupId>1</groupId>
    <groupId>5</groupId>
    <code>13</code>
</return>

为什么(array)$xml->groupId ......哦......哈哈:-)现在我看到了问题....谢谢

1 个答案:

答案 0 :(得分:1)

$xml = '<?xml version="1.0"?><return><groupId>1</groupId><groupId>5</groupId>
    <code>13</code></return>';

$xml = simplexml_load_string($xml);
print_r($xml);
if(in_array(1, (array)$xml)) {
    echo 'got it';
}else {
    echo 'not get';
}