Getting data from php nested objects - php

I am trying to extract data from a php object that is actually retrived from the google blogger post feed. I tried using javascript and it works fine with dot notations. But I am not being able to do same with php.
Here is how the objects look like
stdClass Object
(
[id] => stdClass Object
(
[$t] => tag:blogger.com,1999:blog-8275521076679012362.post-3869147994923309099
)
[published] => stdClass Object
(
[$t] => 2017-12-20T07:02:00.000-08:00
)
[updated] => stdClass Object
(
[$t] => 2018-01-12T07:38:12.068-08:00
)
[category] => Array
(
[0] => stdClass Object
(
[scheme] => http://www.blogger.com/atom/ns#
[term] => stacks
)
)
[title] => stdClass Object
(
[type] => text
[$t] => New #85 & 86: 2day Stack and LastPublished Stack
)
)
There are many but I am showing only few here.
Here is my code that I have tried.
for ($i=0; $i <$len ; $i++) {
$thisPost = $feedData->feed->entry[$i];
print_r($thisPost); // The above object is printed because of this line.
}
The problem is, I am not being able to get the title or anything from the object.

You're probably having trouble because of the $t property names.
If you try to access them with, for example, $thisPost->title->$t, it won't work because PHP will interpret the $t as a variable, which probably doesn't exist.
You can use complex syntax to specify that it's just a string.
$thisPost->title->{'$t'}

Related

Extraction of data from Soap Response in Php

I did a SOAP/WSDL request with a PHP script.
I could access an answer which look like this :
stdClass Object
(
[inventory] =>
(
[0] => stdClass Object
(
[location_inventory] => stdClass Object
(
[location] => stdClass Object
(
[organization] => abcdef
[contact] => stdClass Object
(
[contact_details] => stdClass Object
(
[id] => Thomas
)
)
)
[product] => tomatoes
)
)
)
)
The point is that I can't extract the variables "organization", "id", or "product".
What is the difference of syntax when accessing a stdClass Object or an Array ?
I am guessing you are using json_decode to have the object you showed.
If that is the case, try using json_decode( $data, true); which will return a proper array as described in the docs: here.
That way you will be able to access your data the normal array way: $data['inventory']...
Hope that helps

stdClass Object bing api

I try to take from bing api related results and write script, who take me with print_r($value) this
Array
(
[0] => stdClass Object
(
[__metadata] => stdClass Object
(
[uri] => https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/RelatedSearch?Market='en-US'&Query='car'&$skip=0&$top=1
[type] => RelatedSearchResult
)
[ID] => 8bbe5043-f85b-41b3-b044-3649628fc5cf
[Title] => Cars Games
[BingUrl] => http://www.bing.com/search?q=Cars+Games
)
[1] => stdClass Object
(
[__metadata] => stdClass Object
(
[uri] => https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/RelatedSearch?Market='en-US'&Query='car'&$skip=1&$top=1
[type] => RelatedSearchResult
)
[ID] => b9df73ab-c410-4480-b172-2719643d1120
[Title] => Car Pictures
[BingUrl] => http://www.bing.com/search?q=Car+Pictures
)
)
How i can take [Title] and [BingUrl] from this array ? Thank you.
Use [] to access array elements and use -> to access object members.
In your case, it would be $arr[$index]->Title and $arr[$index]->BingUrl
Most APIs provide results in JSON format, so you may save yourself some headache by using json_decode($api_result, 1) to decode them as an array instead of an object.
If $value is array then you can use this code
foreach ($value as $item)
{
echo $item->Title;
echo $item->BingUrl
}

Getting sugarcrm array output via REST api sorted with PHP correctly

First, I'm using sugarcrm pro 6.5 and accessing via rest v4, so I have this array that's being returned from printing $results that is working fine:
stdClass Object
(
[result_count] => 2000
[total_count] => 3390
[next_offset] => 2000
[entry_list] => Array
(
[0] => stdClass Object
(
[id] => 77da301b-83dd-4fe6-e38f-53ba151fb084
[module_name] => Leads
[name_value_list] => stdClass Object
(
[id] => stdClass Object
(
[name] => id
[value] => 77da301b-83dd-4fe6-e38f-53ba151fb084
)
[name] => stdClass Object
(
[name] => name
[value] => Jim Beam
)
[status] => stdClass Object
(
[name] => status
[value] => Dead
)
[website] => stdClass Object
(
[name] => website
[value] => website.com
)
[phone_cr] => stdClass Object
(
[name] => phone_cr
[value] => 1-888-888-8888
)
)
)
[1] => stdClass Object
(
[id] => d0ecc069-d556-98f3-41f2-53ba1468327a
[module_name] => Leads
[name_value_list] => stdClass Object
(
[id] => stdClass Object
(
[name] => id
[value] => d0ecc069-d556-98f3-41f2-53ba1468327a
)
[name] => stdClass Object
(
[name] => name
[value] => John Doe
)
[status] => stdClass Object
(
[name] => status
[value] => New
)
[website] => stdClass Object
(
[name] => website
[value] => web.com
)
[phone_cr] => stdClass Object
(
[name] => phone_cr
[value] => 1-888-888-8888
)
)
)
I'm using a query from the api to filter the results for the user I'm targeting:
'query' => "leads.assigned_user_id='user_ID-here'",
'order_by' => "date_entered DESC",
This works fine. So I've ran a foreach () statement to retrieve only one field on a button click, which also works just fine. What I really need to accomplish is before this statement, a foreach() command (or something else?) to filter out and retrieve ONLY the "New" results in the status value, and from that group output an array showing only the website field. Seen in the "desired end result section of this question."
This is the code I'm filtering the field I'm targeting and having a new array created with if that helps bridge the gap:
$results = call('get_entry_list', $params, $url);
$eresult = array();
foreach ($results->entry_list as $index=>$value_list) {
$listed = $value_list->name_value_list->website->value;
$eresult[] = $listed;}
So the desired end result based on this data should be:
Array
(
[1] => web.com
)
I'm unsure what I need to do to filter the "Status" field to only then be ran with the $eresult array I created to achieve this. To be clear, everything is working as it should, and my print from $eresult is outputting exactly as it should by returning all results in the website value area, I just need some help to get it sorted before going to that step by sorting it by the "new" status first without all the extra 'stuff,' then sorting out the array in my desired format with the foreach() statement above. I tried to cut out all the other code, as it's a pretty long project, so this should be all the relevant information for the particular goal I need to accomplish in this segment. Any help is greatly appreciated! Thank you!
I've decided to create a second script for this as a temp solution by adding:
'query' => "(leads.assigned_user_id='user_ID-here') AND (status='New')"
So I guess that works, I was trying to avoid calling another script for just one separate function, but it is working fine.

Getting value from std Object

Can anyone please help me out with that one: I'm trying to echo the value from "value" with $someVar->data->values->value but doesn't return anything. I also tried $someVar->values->value. I'm a PHP newbie so please forgive me if that is a silly question.
stdClass Object ( [data] => Array ( [0] => stdClass Object ( [id] =>
123/insights/post_impressions_organic_unique/lifetime [name] =>
post_impressions_organic_unique [period] => lifetime [values] => Array ( [0] =>
stdClass Object ( [value] => 286 ) ) [title] => Lifetime Post Organic Reach [description] => Lifetime The number of people who saw your Page post in News Feed or ticker, or on your Page's Wall. (Unique Users) ) ) [paging] => stdClass Object ( [previous] =>
https://graph.facebook.com/123/insights/post_impressions_organic_unique&access_token=[next] => https://graph.facebook.com/123/insights/post_impressions_organic_unique&access_token= ) )
The code is a JSON decoded output from the Facebook Graph API.
There are arrays within objects there, try this.
foreach($someVar->data as $arr1)
{
foreach($arr1->values as $obj)
{
echo 'Value: ' . $obj->value;
}
}
Also it will help you if you put <pre> tags around a printed object, it formats it so you can easily see the structure.
$someVar->data[0]->values[0]->value

How do I extract a value from this json_decode?

I have got this result using json decode from an api call. but I dont know how to extract "VALUE" from this result..
$obj=json_decode($json_string);
print_r($obj);
stdClass Object ( [status] => OK [data] => stdClass Object ( [trends] => stdClass Object ( [rank] => Array ( [0] => stdClass Object ( [date] => 201011 [value] => 7196 ) ) ) [trends_low_sample] => [query_cost] => 1 [trends_frequency] => monthly ) )
I need only "7196" from this result. how do I do this??
Ah! Based on your updated code you're tring to get the value from PHP not Javascript? Personally I use json_decode($json_string,true); to get an associative array (json_decode), if you do that it should be accessible as:
echo $obj["data"]["trends"]["rank"][0]["value"];
As an object it's accessible as:
echo $obj->data->trends->rank[0]->value;

Categories