Can someone help me retrieve the value of the id from this array? Thanks
Array
(
[#attributes] => Array
(
[id] => 1
)
)
This is a SimpleXML element. The #attributes key is just how it's set internally. Don't try to access it via that key.
You can just simply get the attribute by doing:
$id = (string)$element['id'];
Note: The cast to a string is needed because SimpleXML gives you a SimpleXMLElement object when you access something.
Related
I have an array, containing an object. I need the value of a property of the first object but somehow I get an empty value.
My array $params (from print_r) looks like this:
Array
(
[newOrderStatus] => OrderState Object
(
[name] => Canceled
[template] => order_canceled
[send_email] => 1
...
Cut off here, there are two more objects in this array.
Now if I do: echo $params[0]->name I get an empty result.
Also tried print_r($params[0], true);, empty result.
Also tried, empty result:
$status = $params[0];
echo $status->name;
What am I doing wrong here?
Thanks in advance
Well, as you said your array looks like this :
Array
(
[newOrderStatus] => OrderState Object
(
[name] => Canceled
[template] => order_canceled
[send_email] => 1
...
So there is no $param[0], you should do $param['newOrderStatus'] and then get what you want : $param['newOrderStatus']->name
You need to access object as following
$params['newOrderStatus'];
In above object you will have all child objects so you can access them by following
$params['newOrderStatus']->name;
$params['newOrderStatus']->template;
Your array $params has a key called newOrderStatus which has the object as a value you are looking for.
Looking at your example, there is value for index 0.
To get the value of the name property, you could use:
$params['newOrderStatus']->name
You can type cast it to an array like this:
$array = (array) $yourObject;
I sometimes encounter and object like this:
stdClass Object
(
[batchcomplete] =>
[query] => stdClass Object
(
[pages] => stdClass Object
(
[48548] => stdClass Object
(
[pageid] => 48548
[ns] => 0
[title] => Dopamine
That object key 48548 is gonna be different every time so I have no way of knowing what its value is. Lets say I need to get the title (Dopamine) in this object, I would need to do something like this:
$title = $object->query->page->{*WILDCARD*}->title;
But I haven't figured out a way to do this yet. Is there a way to skip an object key like this without having to find out the value of the key?
A numeric object property is not going to work. Assuming there is only one, convert to an array and get the key:
$array = (array)$object->query->pages;
$title = $array[key($array)]->title;
Or just get the one element:
$title = current((array)$object->query->pages)->title;
If this is coming from JSON you might want to decode it as an array in the first place. If not, then maybe this:
$array = json_decode(json_encode($oject), true);
For non-numeric properties this should work:
$var = key(get_object_vars($object->query->pages));
$title = $object->query->pages->$var->title;
How do I get the values of the follow array fields in php? When I do a print_r function on a two dimensional array, I get the following result:
[title] => SimpleXMLElement Object ( ) [date] => SimpleXMLElement Object ( )
How do I get the string value of title and date? I've tried casting them with
(string)
, however this didn't work.
You need to access the attributes of the SimpleXMLElement Object.
Say you have an instance named entry with child title you type
(string) $entry->title;
I'm getting stumped by simply trying to get the key/value of a certain tier inside an array I have created from an XML file. The part of the array from Print_R() is:
SimpleXMLElement Object
(
Array
(
[category] => SimpleXMLElement Object
(
[#attributes] => Array
(
[settings] => maximum
)
[cat_1] => 5.21
[cat_2] => 5.05
[cat_3] => 19.36
[cat_4] => 21.97
[cat_5] => 12.17
)
)
)
I am trying to get the "cat_1, cat_2, cat_3 .." keys so that I can put them in their own array and use them for other things. I can do print_r($array) and it works, but when I try and do this:
foreach ($array->category as $key => $val) {
$new_array[$key]= "$val";
}
$array->category doesn't seem to target that list. The "SimpleXMLElement Object" from the XML file seems to be in the way of how I normally use arrays. Does anyone know how I can get to those cat_1 ets. lists?
Notice that $array->category is an object, not an array and cat_* are properties. Since they are all public just use:
$new_array = get_object_vars($array->category);
You should convert simpleXML object to array using
$array = json_decode(json_encode((array) $simplexmlob)), 1);
Then use $array['category'] for other things. There is no need to use foreach loop.
How do i extract the data from that xml object which is a value of a certain array:
Array ( [Title] => SimpleXMLElement Object (
[0] => The Key of Life; A Metaphysical Investigation )
[ASIN] => SimpleXMLElement Object ( [0] => 0982385099 ) ...
Do string typecasting of the object.
$variable = (string) $FieldValue[0];
That would work, as SimpleXml has all the children in object type and not string.
say $X is the object, so to print
The Key of Life; A Metaphysical
Investigation
you do:
echo $X->Title[0]
It's important to understand that you're not working with an array — you're working with a SimpleXMLElement object, which is not the same.
Instead of doing $array['key']['subkey'], you would do $xml->tag->subtag.
SimpleXML nodes are not strings or arrays, although they behave string-like and array-like. Make sure you always typecast the value to an explicit string.
If you're accessing the first node, you don't need to use [0]. It's assumed.
You can convert SimpleXMLElement objects into true associative arrays in PHP 5.2 or newer with:
$array = json_decode(json_encode($xml), true);