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.
Related
I'm trying to access values from a PHP object returned from an API. I am trying to get the 'total' atribute.
stdClass Object
(
[#attributes] => stdClass Object
(
[status] => ok
)
[invoices] => stdClass Object
(
[#attributes] => stdClass Object
(
[page] => 1
[per_page] => 25
[pages] => 1
[total] => 5
)
My returned object is stored in a variable called $list.
$list->invoices->attributes->total
I'm trying to echo / print_r that, but getting nothing?
Any help is appreciated!
The # is a part of the property name, you can't just ignore it.
echo $list->invoices->{'#attributes'}->total;
$total = $list->invoices->attributes()->total;
As it turns out, you don't need to even specify #attributes to read this data. The key trick to get to it is to cast the result as a string.
So, I know it seems strange, but this will work in this case:
echo (string)$list->invoices['total'];
In my case i used:
$att_side = $xml->item->attributes()->side;
I am trying to get specific data set from the object but unable to find out why I cannot call the number in my assignment. This is it as follows:
$teir = $league->data->summonerLeagues->0->teir;
First of all this is calling the data from the league which is set and so that yo can see what the data looks like here it is:
stdClass Object
(
[data] => stdClass Object
(
[summonerLeagues] => Array
(
[0] => stdClass Object
(
[queue] => RANKED_SOLO_5x5
[name] => Dr. Mundo's Crushers
[tier] => BRONZE
[requestorsRank] => III
[entries] => Array
at this point I am trying to assign the variable $teir to teir in the object but they use 0 in the object and the way I am calling it must be the issue.....
Any suggestions?
Array access is with brackets, while object properties are accessed with ->:
$tier = $league->data->summonerLeagues[0]->tier; // Fixed typo per #MikePurcell's comment
Say I have a variable called $myXMLElement and when I dump the variable it looks like this.
SimpleXMLElement Object (
[#attributes] => Array ( [id] => 7 )
[0] => Kirkpatrick
)
How do I grab the value of Kirkpatrick in PHP
Have you tried:
(string) $var[0]
? Normally you do that with string casting, will turn SimpleXMLElements into the XML nodeValue.
This should get the value:
echo $myXMLElement->{'0'};
When I print_r($var) I get the result below.
SimpleXMLElement Object
(
[SEND_FILE] => SimpleXMLElement Object
(
[FILEID] => 123
[GUID] => 456
[SUMMARY] => SimpleXMLElement Object
(
[NB_PAYMENTS] => 1
)
)
)
How can I get the value of the FILEID element in a variable? If I do
print $result->SEND_FILE->FILEID[0]
then I just get the number - what I want, no mention of a SimpleXML Object.
But if I put this variable in an array, as such
$res['file_id'] = $result->SEND_FILE->FILEID[0]
and then print_r($res) I get:
Array
(
[file_id] => SimpleXMLElement Object
(
[0] => 307466
)
)
How can I get it to remove the [0] / SimpleXMLElement Object?
This will look not too elegant, but try casting the result to integer (if the type is known):
$res['file_id'] = (int)$result->SEND_FILE->FILEID[0]
Why do you append the [0] at the end? You dont need that. You should simply do
print $result->SEND_FILE->FILEID;
And that should be enough.
This is driving me crazy, I am trying to get to a specific part of this object and it is driving me crazy, here is the object contents:
XMLHandler Object
(
[doc:XMLHandler:private] => SimpleXMLElement Object
(
[#attributes] => Array
(
[state] => Live
)
[newsListItem] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[href] => http://api.contentplus.co.uk/6cb5ea15-d6b1-4c40-9db7-cb2a3315080b/news/800773226/
)
[id] => 800773226
[publishDate] => 2011-10-24T10:04:49
[lastModifiedDate] => 2011-10-24T11:20:40
[headline] => Relationships matter on social media
)
)
)
[format] => html
)
I want to get the value of [id] I am trying to access it like this:
echo $niList->doc->newsListItem[0]->id;
but this is giving me nothing, I know I am close (well I hope I am) but I just cant quite get it right, could anyone help please.
Thanks all.
Your object dump says
doc:XMLHandler:private
which means doc is a private property of XMLHandler. As such, you can only access it from within that object via $this. But you are trying to access it from outside the object when you do
echo $niList->newsListItem[0]->id;
This wont work. Add a method to that XMLHandler object that does what you want to do with that newslistitem id. Also see the chapter on Visibility in the PHP Manual:
http://docs.php.net/manual/en/language.oop5.visibility.php