1

I have the following object:

object(SimpleXMLElement)#337 (1) { [0]=> string(4) "1001" }

But I can't seem to access it using [0] or even not using foreach($value as $obj=>$objvalue)

What am I doing wrong?

1
  • Try casting it to string: (string)$element Commented Jul 12, 2012 at 8:40

3 Answers 3

1

SimpleXMLElement implements Traversable, which means you could use foreach to loop it.

Sign up to request clarification or add additional context in comments.

Comments

0

Try to use

$objectarray = get_object_vars(object(SimpleXMLElement));

Comments

0

By looking into the SimpleXMLElement manual I found this example (the example XML file is on the top of the page of the link):

$movies = new SimpleXMLElement($xmlstr);

/* For each <character> node, we echo a separate <name>. */
foreach ($movies->movie->characters->character as $character) {
   echo $character->name, ' played by ', $character->actor, PHP_EOL;
}

And I found this function to transform the XML object to an array, maybe that's easier to use?:

    function toArray($xml) { //$xml is of type SimpleXMLElement 
        $array = json_decode(json_encode($xml), TRUE);

        foreach ( array_slice($array, 0) as $key => $value ) {
            if ( empty($value) ) $array[$key] = NULL;
            elseif ( is_array($value) ) $array[$key] = toArray($value);
        }

        return $array;
    }

Comments

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.