When I print_r() the SimpleXMLElement Object referenced by variable $xmlObject, I see the following structure:
SimpleXMLElement Object
(
[#attributes] => Array
(
[uri] => /example
)
[result] => SimpleXMLElement Object
(
[message] => Record(s) added successfully
[recorddetail] => Array
(
[0] => SimpleXMLElement Object
...
)
)
)
Notice how the $xmlObject->result->message property looks like it is just a string. However, if I do print_r($xmlObject->result->message), I get the following:
SimpleXMLElement Object
(
[0] => Record(s) added successfully
)
So at this point I'm confused. Why is $xmlObject->result->message being identified as an instance of SimpleXMLElement Object in this case, when the result of printing the full $xmlObject doesn't suggest this?
And how do I actually access this value? I've tried $xmlObject->result->message[0], but it just prints out the same thing (i.e. the last code snippet I posted).
The representation you get when using print_r or var_dump on a SimpleXMLElement has very little to do with how it is structured internally. For instance there is no property #attributes you could access with $element['#attributes']['uri'] either. You just do $element['uri']
This is simply the way it is. SimpleXmlElement objects behave different. Make sure you read the examples in the PHP Manual before using SimpleXml:
http://php.net/manual/en/simplexml.examples-basic.php
To understand the implementation it in detail, you'd have to look at the source code:
http://lxr.php.net/opengrok/xref/PHP_TRUNK/ext/simplexml/simplexml.c
To print $xmlObject->result->message you just do echo $xmlObject->result->message. That will autocast the SimpleXmlElement to string.
Related
I am stuck in a problem, I want to access a value which comes under the SimpleXMLElement Object under #attributes category, here is the array:
SimpleXMLElement Object (
[#attributes] => Array (
[src] => source/send_smsf.php
[name] => mainFrame
[id] => mainFrame
[title] => mainFrame
)
)
I want to access the id attribute inside it. But how I can do that?
I got the answer :
$id = $mainNode->attributes()->id;
We can place the #attribute in attributes function and then can call any value inside.
You can simply get that object to a variable and access it like this,
$id = (string) $object['id']; //$object is the SimpleXMLElement object.
I have a RSS object $rssObject created using the PHP simplexml_load_file function, the goal is to get the value of [href].
var_dump($rssObject) returns the following:
Array
(
[0] => SimpleXMLElement Object
(
[link] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[href] => https://www.example.com
)
)
I have tried unsuccessfully to get the contents of [href] using this notation which returns null
$rssObject[0]->link[0]->{'#attributes'}['href'];
Not sure why?
Any help would be appreciated!
In SimpleXML, attributes are accessed using array notation:
$xml = simplexml_load_file();
$url = $xml[0]->link[0]['href'];
See "Example #5 Using attributes" in the PHP manual.
Am trying to access the attribute of the simple xml
SimpleXMLElement Object
(
[#attributes] => Array
(
[Index] => 21
)
[Data] => Hello world
)
i want to access Index attribute. I tried the following code but its not
working for me
$xml->attributes()->Index
There you go my friend:
$xml->attributes()['Index']
Sometimes I noticed I had to cast the result when it's a string. Like:
(string)$xml->attributes()['Index']
Hello I have this json:
[{"org_src":"img\/base\/logo.png","src":"\/cache\/300-logo.png"},
{"org_src":"\/img\/l2.JPG","src":"\/cache\/6l-2.JPG"},
{"org_src":"\/img\/studio\/desk.JPG","src":"\/cache\/desk.JPG"},
...
How looks the array before its is decoded?
If I use Json_encode to this, I will get this:
Array
(
[0] => stdClass Object
(
[og_src] => img/base/logo.png
[src] => /cache/300-logo.png
)
[1] => stdClass Object
(
[og_src] => /img/l2.JPG
[src] => /cache/6l-2.JPG
)...
What is stdClass Object?
Thanks for any help in advance.
with json_decode($test,true); I will get this:
Array
(
[0] => Array
(
[og_src] => img/base/logo.png
[src] => /cache/logo.png
)
[1] => Array
(
[og_src] => /img/studio/l2.JPG
[src] => /cache/6l-2.JPG
...
This do not help me to see how the origin array looks like.
Here is the answer. That what I looked for. Thanks for any suggestion.
$stack[0][org_src]= "Hallo";
$stack[0][src] = "scrkjh";
$stack[1][org_src] = "Halfgfglo";
$stack[1][src] = "scrkjh";
json_encode($stack);
You probably meant json_decode
If you look in the docs, you will notice that there is assoc parameter if you want to get associative array instead of an object.
So you should do
$data = json_decode($data, true);
if you don't want objects
stdClass is the base class of all objects in PHP. If functions like json_decode() create objects that are not of a special class type or other data types will be casted to object, stdClass is used as the data type.
You can compare it to the Object data type in Java.
You asked for an example how to access stdClass's properties, or in general, object properties in PHP. Use the -> operator, like this:
$result = json_decode($json);
// access 'src' property of first result:
$src = $result[0]->src;
stdClass is php's generic empty class, kind of like Object in Java or object in Python
It is useful for anonymous objects, dynamic properties, etc. and Stdclass Object is its object
See Dynamic Properties in PHP and StdClass for example
Also.. As Marko D said, You can use json_decode($data, true); true to get an associative array instead of object.
I have some xml, lets say <names number="12"></names>
When I run the following:
$simpleXMLElement = new SimpleXMLElement($xml);
pr($simpleXMLElement);
I get the following:
SimpleXMLElement Object
(
[#attributes] => Array
(
[number] => 12
)
[0] =>
)
It throws in that 0 entry. This is weird. I don't know what it's supposed to represent. If I do this instead:
<names number="12"><name first="oliver" /></names>
I get the following output:
SimpleXMLElement Object
(
[#attributes] => Array
(
[number] => 12
)
[name] => SimpleXMLElement Object
(
[#attributes] => Array
(
[first] => oliver
)
)
)
This is as expected (for me at least). Any thoughts/direction?
First: if you don't correctly format your post, the XML will not be displayed. Indent any code with at least 4 spaces.
Secondly, do not expect print_r() or var_dump() to give you an exact representation of a SimpleXMLElement because SimpleXML uses lots of magic, so children and attributes won't necessarily show up in the output.
It seems to be just SimpleXML doing a quick-and-dirty job of parsing the element: Since you have <names></names>, it adds an array inside the element, as expecting elements within it, and when it doesn't find any elements inside the names tags, it leaves an empty array, with the key 0, since it doesn't know what name to give it.
A short tag (<names />) shouldn't generate the empty content. (As weird as that sounds.)