user137621
user137621

Reputation:

PHP, json_encode, json_decode of SimpleXML Object

A function in my application does the following:

My problem arises when recovering this block from the database, and decoding it. I can see the @attributes when I var_dump the object, but cannot find a combination of commands that allows me to access them.

Error message is: Fatal error: Cannot use object of type stdClass as array

Below is a sample of my object. I have tried, amongst other what used to work.

echo $obj['class'];

stdClass Object
(
    [@attributes] => stdClass Object
        (
            [class] => race_idx_hdr
        )

    [img] => stdClass Object
        (
            [@attributes] => stdClass Object
                (
                    [src] => /Images/Icons/i_blue_bullet.gif
                    [alt] => image
                    [title] => United Kingdom
                )

        )

    [a] => Fast Cards
)

Upvotes: 4

Views: 7445

Answers (3)

Henrik Opel
Henrik Opel

Reputation: 19441

When you decode the json from the database, you get an object of type 'stdClass' instead of the original type 'SimpleXMLElement' returned by the SimpleXMLElement::xpath function.

The stdClass object does not 'know' about the pseudo array syntax used by SimpleXMLElement objects to allow accessing the attributes.

Normally you would use the serialize() and unserialize() functions instead of json_encode/decode to store objects in a database, but unfortunately, SimpleXMLElements are not working with those.

As an alternative, why not just store the actual xml and read it back to SimpleXML after fetching it from the database:

// convert SimpleXMLElement back to plain xml string
$xml = $simpleXML->asXML();

// ... code to store $xml in the database
// ... code to retrieve $xml from database

// recreate SimpleXMLELement
$simpleXML = simplexml_load_string($xml);

Upvotes: 3

Stefan Gehrig
Stefan Gehrig

Reputation: 83652

I actually don't really understand what you're trying to do and where the error is thrown, but to access the properties of your object you can use

echo $obj->{'@attributes'}->class; // prints "race_idx_hdr"
echo $obj->img->{'@attributes'}->src; // prints "/Images/Icons/i_blue_bullet.gif"
echo $obj->img->{'@attributes'}->alt; // prints "image"
echo $obj->img->{'@attributes'}->title; // prints "United Kingdom"
echo $obj->a; // prints "Fast Cards"

This weird syntax ($obj->{'@attributes'}) is required because the @-symbol is reserved in PHP and cannot be used for identifiers.

Upvotes: 3

Lawrence Barsanti
Lawrence Barsanti

Reputation: 33342

If an object is converted to an array, the result is an array whose elements are the object's properties.

$asArray = (array)$myObj;
echo $asArray['@attribute'];

Upvotes: 0

Related Questions