0

print_r($xml);:

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

in_array(1, $xml->groupId) on this doesn't work: PHP Warning: in_array() expects parameter 2 to be array, object given

print_r((array)$xml->groupId); prints only first element in array:

Array
(
    [0] => 1
)

How can I properly check for an element existing in groupId, without a hack like 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>

Why does (array)$xml->groupId .... oh ... lol :-) Now I see the problem.... thanks

2
  • can you also post your xml? try $xml->{groupId} Commented May 23, 2014 at 10:12
  • see link this would be help you. Commented May 23, 2014 at 10:16

1 Answer 1

1

try

$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';
}
Sign up to request clarification or add additional context in comments.

3 Comments

When I looked at the XML, I noticed it is not like I expected. (array)$xml was correct, not (array)$xml->groupId :-) Can you update the answer a little bit for me to accept, thank you.
@DanFromGermany thanks i am confused on my answer cause you have also a good ranking on stack
No problem, I was confused too ;) Have a nice day

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.