PHP stdClass Object arrays [_]=> syntax - php

I am trying to access the 1st part of the following Object.
I'm unable to figure out what is the [_] => syntax?
If I try to print the 1st past of the Object using Body->[_] it errors out.
[Body] => stdClass Object
(
[_] => http://xyz.com
http://MPQ.com
[BodyType] => Text
)

Try this:
$object->_
_ is a valid variable name in PHP.

Related

PHP HTML Tags.and json_encode and json_decode. remove linebreaks in JSON

I have an array width an object which Iam trying to json_decode: My problem is that iam not sure how to make the right json_encoding and afterwords decode.
Array (
[id] => 22
[infotext] => {"2":"<p>da</p>
","3":"<p>en</p>
"}
[language_status] => {"2":{"status":"0"},"3"}
)
Decoding the [language_status] gives me:
var_dump(json_decode($arr['language_status']))
stdClass Object (
[2] => stdClass Object (
[status] => 0
)
[3] => stdClass Object (
[status] => 0
)
)
which is fine, but my problem is that i can't seem to get an output when json decoding [infotext].
Iam sure it is because the html tags, but just cant get the right input/output the get this to work.
I would love to see the [infotext] output somewhere like this:
var_dump(json_decode($arr['infotext']))
stdClass Object (
[2] => <p>da</p>
[3] => <p>en</p>
)
Please help me solve this
Ok i managed to recreate my problem and i found out its when i create the Json object the problem occurs: http://codepad.viper-7.com/SG175W
As you can see the JSon object has breaks in the array which is creating the problem.
Any easy way to remove em?
Try this,
var_dump(json_decode($arr['language_status']),true);
This must work, maby you can change it in your way.
<?php
$infotext = json_decode('{"2":"<p>da</p>","3":"<p>en</p>"}');
foreach($infotext as $text){
echo $text;
}
?>

Accessing #attribute from php object

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;

php: trying to call data from an object

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

how to get to a specific part of this nested array/object

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

Parsing PHP array which has an abject inside

I've got a response form the solr query in php. below is the response form apache solr in drupal6, i need to access the id field inside the Apache_Solr_Document, can some bosy help me with this.
i was able to print this using print_r($result);
Array ( [type] => Bookmarks [node] => Apache_Solr_Document Object ( [_documentBoost:protected] => [_fields:protected] => Array ( [id] => b17692e4ad53/node/274 )))
if i do print_r($result[node]); i am getting
Apache_Solr_Document Object ( [_documentBoost:protected] => [_fields:protected] => Array ( [id] => b17692e4ad53/node/274 ))
from here i can't figure out how to access the id.
Have you tried reading through IBM's article on Solr? The end of the article deals specifically with PHP.
Edit: After finding a source class to read through, it looks like you can do:
$result['node']->getField("id");
or using the magic __get:
$result['node']->id;
You can try using the {} brackets if the object name is not a valid object property name (like SimpleXML stuff).
$object->{'strang&$*#nmame'}->value;

Categories