Having problems with recursive array objects (stdClass Object) - php

I'm hoping someone can help me with my syntax. I think that's the issue. I'm trying to have the loop go through display the chosen object, but it's not working. Here is my code.
stdClass Object
(
[result_count] => 2
[total_count] => 2
[next_offset] => 2
[entry_list] => Array
(
[0] => stdClass Object
(
[id] => da2b0ab5-873b-d9a8-0fcd-5180871e57eb
[module_name] => Project
[name_value_list] => stdClass Object
(
[description] => stdClass Object
(
[name] => description
[value] => soccer is fun!!!
)
[priority] => stdClass Object
(
[name] => priority
[value] => Primary
)
[total_years_played_c] => stdClass Object
(
[name] => total_years_played_c
[value] => 5
)
[sport_c] => stdClass Object
(
[name] => sport_c
[value] => Soccer
)
[high_school_exp_c] => stdClass Object
(
[name] => high_school_exp_c
[value] => 2
)
[varsity_exp_c] => stdClass Object
(
[name] => varsity_exp_c
[value] => default
)
[hand_right_c] => stdClass Object
(
[name] => hand_right_c
[value] => 0
)
[hand_left_c] => stdClass Object
(
[name] => hand_left_c
[value] => 1
)
[foot_right_c] => stdClass Object
(
[name] => foot_right_c
[value] => 1
)
[foot_left_c] => stdClass Object
(
[name] => foot_left_c
[value] => 0
)
[specialskills_c] => stdClass Object
(
[name] => specialskills_c
[value] =>
)
[varsity_starter_c] => stdClass Object
(
[name] => varsity_starter_c
[value] => 0
)
)
)
[1] => stdClass Object
(
[id] => 2e805552-c887-2b2e-a9e5-518087f3f87a
[module_name] => Project
[name_value_list] => stdClass Object
(
[description] => stdClass Object
(
[name] => description
[value] => I bleed for hockey... I will teach your kid to put them on the boards and take no prisoners!
)
[priority] => stdClass Object
(
[name] => priority
[value] => Secondary
)
[total_years_played_c] => stdClass Object
(
[name] => total_years_played_c
[value] => 7
)
[sport_c] => stdClass Object
(
[name] => sport_c
[value] => Hockey
)
[high_school_exp_c] => stdClass Object
(
[name] => high_school_exp_c
[value] => 3
)
[varsity_exp_c] => stdClass Object
(
[name] => varsity_exp_c
[value] => 2
)
[hand_right_c] => stdClass Object
(
[name] => hand_right_c
[value] => 0
)
[hand_left_c] => stdClass Object
(
[name] => hand_left_c
[value] => 1
)
[foot_right_c] => stdClass Object
(
[name] => foot_right_c
[value] => 1
)
[foot_left_c] => stdClass Object
(
[name] => foot_left_c
[value] => 0
)
[specialskills_c] => stdClass Object
(
[name] => specialskills_c
[value] =>
)
[varsity_starter_c] => stdClass Object
(
[name] => varsity_starter_c
[value] => 0
)
)
)
)
[relationship_list] => Array
(
)
)
Here is the PHP that I am using. For now, I'm just wanting to echo the name of sport_c (one should be Hockey, the other should be Soccer)
<?
foreach($response->entry_list as $sports) {
if(!empty($sports->name_value_list->sport_c)) {
$sport_list = $sports->name_value_list->sport_c;
$sport_name = $sport_list->value;
}
}
echo $sport_name;
?>

Put the echo statement within the foreach loop:
foreach($response->entry_list as $sports) {
if(!empty($sports->name_value_list->sport_c)) {
$sport_list = $sports->name_value_list->sport_c;
$sport_name = $sport_list->value;
}
echo $sport_name;
}

Related

PHP parse soapClient response transfer to a table

I'm just on the hose and dont get on.
I am not a professional programmer but have so far with much reading and over numerous attempts everything so far created what I have undertaken, only with this I have probably found my masterpiece.
I have a response of a SOAP query and would like to display the values in a table for each powerUnitidentifier. What is the best way to do this?
(
[RawData] => stdClass Object
(
[from] => 2022-05-10T01:00:00+02:00
[to] => 2022-05-10T01:20:00+02:00
[dataRecords] => stdClass Object
(
[record] => Array
(
[0] => stdClass Object
(
[powerUnitIdentifier] => abc123
[time] => 2022-05-10T01:00:00+02:00
[fields] => stdClass Object
(
[field] => Array
(
[0] => stdClass Object
(
[identifier] => 100
[value] => 0
)
[1] => stdClass Object
(
[identifier] => 101
[value] => 3.27
)
[2] => stdClass Object
(
[identifier] => 102
[value] => 70.00
)
)
)
)
[1] => stdClass Object
(
[powerUnitIdentifier] => zyx321
[time] => 2022-05-10T01:00:00+02:00
[fields] => stdClass Object
(
[field] => Array
(
[0] => stdClass Object
(
[identifier] => 100
[value] => 0
)
[1] => stdClass Object
(
[identifier] => 101
[value] => 3.19
)
[2] => stdClass Object
(
[identifier] => 102
[value] => 70.00
)
)
)
)
[2] => stdClass Object
(
[powerUnitIdentifier] => abc123
[time] => 2022-05-10T01:10:00+02:00
[fields] => stdClass Object
(
[field] => Array
(
[0] => stdClass Object
(
[identifier] => 100
[value] => 0
)
[1] => stdClass Object
(
[identifier] => 101
[value] => 3.15
)
[2] => stdClass Object
(
[identifier] => 102
[value] => 70.00
)
)
)
)
[3] => stdClass Object
(
[powerUnitIdentifier] => zyx321
[time] => 2022-05-10T01:10:00+02:00
[fields] => stdClass Object
(
[field] => Array
(
[0] => stdClass Object
(
[identifier] => 100
[value] => 0
)
[1] => stdClass Object
(
[identifier] => 101
[value] => 3.09
)
[2] => stdClass Object
(
[identifier] => 102
[value] => 70.00
)
)
)
)
)
)
)
)```
You loop over the section of the data, starting the foreach loop at the right level of your data structure
foreach($theName->RawData->DataRecords->record as $obj) {
echo $obj->powerUnitIdentifier;
}
Or if you ment to process the sub array of that
foreach($theName->RawData->DataRecords->record as $obj) {
echo $obj->powerUnitIdentifier . '<br>';
foreach( $obj->fields as $field) {
echo $field->identifier . ',' . $field->value . '<br>';
}
}

Sorting multi-dimensional array by count of subarray

I have the array like this:
Array
(
[28748] => stdClass Object
(
[uid] => 28748
[status] => 1
[children] => Array
(
[29163] => stdClass Object
(
[uid] => 29163
[status] => 1
)
)
)
[28708] => stdClass Object
(
[uid] => 28708
[status] => 1
[children] => Array
(
[27104] => stdClass Object
(
[uid] => 27104
[status] => 1
[children] => Array
(
[28250] => stdClass Object
(
[uid] => 28250
[status] => 1
)
)
)
[29448] => stdClass Object
(
[uid] => 29448
[status] => 1
[children] => Array
(
[28528] => stdClass Object
(
[uid] => 28528
[status] => 1
)
[28329] => stdClass Object
(
[uid] => 28329
[status] => 1
[children] => Array
(
[28533] => stdClass Object
(
[uid] => 28533
[status] => 1
)
)
[26548] => stdClass Object
(
[uid] => 26548
[status] => 1
)
)
)
)
)
)
And I want to sort the array by the count of sub array, so the no of children is greater will come first. The level of dimension is unlimited. In my case I want to become this array:
Array
(
[28708] => stdClass Object
(
[uid] => 28708
[status] => 1
[children] => Array
(
[29448] => stdClass Object
(
[uid] => 29448
[status] => 1
[children] => Array
(
[28329] => stdClass Object
(
[uid] => 28329
[status] => 1
[children] => Array
(
[28533] => stdClass Object
(
[uid] => 28533
[status] => 1
)
)
[28528] => stdClass Object
(
[uid] => 28528
[status] => 1
)
[26548] => stdClass Object
(
[uid] => 26548
[status] => 1
)
)
)
[27104] => stdClass Object
(
[uid] => 27104
[status] => 1
[children] => Array
(
[28250] => stdClass Object
(
[uid] => 28250
[status] => 1
)
)
)
)
)
[28748] => stdClass Object
(
[uid] => 28748
[status] => 1
[children] => Array
(
[29163] => stdClass Object
(
[uid] => 29163
[status] => 1
)
)
)
)
Here is the new array that i am checking:
Array
(
[27104] => stdClass Object
(
[uid] => 27104
[status] => 1
[children] => Array
(
[28250] => stdClass Object
(
[uid] => 28250
[status] => 1
[children] => Array
(
[28839] => stdClass Object
(
[uid] => 28839
[status] => 1
[children] => Array
(
[27102] => stdClass Object
(
[uid] => 27102
[status] => 1
)
)
)
)
)
[26551] => stdClass Object
(
[uid] => 26551
[status] => 1
[children] => Array
(
[25368] => stdClass Object
(
[uid] => 25368
[status] => 1
)
)
)
)
)
[28708] => stdClass Object
(
[uid] => 28708
[status] => 1
[children] => Array
(
[29448] => stdClass Object
(
[uid] => 29448
[status] => 1
[children] => Array
(
[28528] => stdClass Object
(
[uid] => 28528
[status] => 1
)
[28329] => stdClass Object
(
[uid] => 28329
[status] => 1
[children] => Array
(
[28654] => stdClass Object
(
[uid] => 28654
[status] => 1
)
)
)
[26548] => stdClass Object
(
[uid] => 26548
[status] => 1
)
)
)
)
)
[28748] => stdClass Object
(
[uid] => 28748
[status] => 1
[children] => Array
(
[28838] => stdClass Object
(
[uid] => 28838
[status] => 1
)
[28685] => stdClass Object
(
[uid] => 28685
[status] => 1
)
[29163] => stdClass Object
(
[uid] => 29163
[status] => 1
)
)
)
)
and after using "sortByNumChildren($data)" i'm getting
Array
(
[28748] => stdClass Object
(
[uid] => 28748
[status] => 1
[children] => Array
(
[29163] => stdClass Object
(
[uid] => 29163
[status] => 1
)
[28685] => stdClass Object
(
[uid] => 28685
[status] => 1
)
[28838] => stdClass Object
(
[uid] => 28838
[status] => 1
)
)
)
[28708] => stdClass Object
(
[uid] => 28708
[status] => 1
[children] => Array
(
[29448] => stdClass Object
(
[uid] => 29448
[status] => 1
[children] => Array
(
[26548] => stdClass Object
(
[uid] => 26548
[status] => 1
)
[28329] => stdClass Object
(
[uid] => 28329
[status] => 1
[children] => Array
(
[28654] => stdClass Object
(
[uid] => 28654
[status] => 1
)
)
)
[28528] => stdClass Object
(
[uid] => 28528
[status] => 1
)
)
)
)
)
[27104] => stdClass Object
(
[uid] => 27104
[status] => 1
[children] => Array
(
[26551] => stdClass Object
(
[uid] => 26551
[status] => 1
[children] => Array
(
[25368] => stdClass Object
(
[uid] => 25368
[status] => 1
)
)
)
[28250] => stdClass Object
(
[uid] => 28250
[status] => 1
[children] => Array
(
[28839] => stdClass Object
(
[uid] => 28839
[status] => 1
[children] => Array
(
[27102] => stdClass Object
(
[uid] => 27102
[status] => 1
)
)
)
)
)
)
)
)
and this looks incorrect as this is not sorted by all children's count.
sorting should be according to the number of all descendants.
Please check this.
thank you so much for your answer #trincot .
but i think one thing i missed here
if the total number of children is greater then that array will come first.
As mentioned in my array suppose i have added 2 more elements in the first sub array likewise
28748 => stdClass Object
(
[uid] => 28748
[status] => 1
[children] => Array
(
[29163] => stdClass Object
(
[uid] => 29163
[status] => 1
)
[29173] => stdClass Object
(
[uid] => 29173
[status] => 1
)
[29174] => stdClass Object
(
[uid] => 29174
[status] => 1
)
)
)
total no of children in first sub array = 3
total no of children in second sub array = 7
but total no of children(counting inner children as well) in the second sub array is greater so the second sub array will come first and this shold work same as in the inner part as well.
Please look into this.
You could use a recursive function that sorts each level with uasort:
function sortByNumChildren(&$array) {
foreach ($array as $key => &$obj) {
if (isset($obj->children)) sortByNumChildren($obj->children);
}
uasort($array, function ($a, $b) {
if (!isset($a->children)) return 1;
if (!isset($b->children)) return -1;
return count($b->children) - count($a->children);
});
}
Call this function as follows:
sortByNumChildren($data);
See it run, together with output, on eval.in
This code will sort by number of (immediate) children. See below for sorting on number of descendants.
Code for sorting by number of all descendants
As requested here is an alternative piece of code which sorts the nested array by number of descendants, so also counting grandchildren and their children, etc.:
function sortByNumChildren(&$array) {
$childCounts = array();
foreach ($array as &$obj) {
$childCounts[$obj->uid] = isset($obj->children) ?
sortByNumChildren($obj->children) : 0;
}
uasort($array, function ($a, $b) use ($childCounts) {
return $childCounts[$b->uid] - $childCounts[$a->uid];
});
return count($array) + array_sum($childCounts);
}
Call this function as follows:
sortByNumChildren($data);
See it run, together with output, on eval.in

How to fetch data from array and insert to database

Hello i Have a problem related to array,i am trying to request graph api and saves data in database.here is my array
Array
(
[threadId] => t_mid.1445648572476:316f06a4adc6d7dd57
[sender] => Array
(
[0] => Array
(
[0] => stdClass Object
(
[name] => rajesh Prasad Shah
[email] => 543634645764577#facebook.com
[id] => 543634645764577
)
[1] => stdClass Object
(
[name] => rjtest
[email] => 849929567895403#facebook.com
[id] => 849929567895403
)
)
)
[messages] => Array
(
[messages] => Array
(
[0] => Array
(
[message] => Same to u dear.
[from] => stdClass Object
(
[name] => rajesh Prasad Shah
[email] => 543634645764577#facebook.com
[id] => 543634645764577
)
[created_time] => 2015-12-31T11:31:24+0000
[id] => m_mid.1451561484487:5b3276ddd9d86d1480
[to] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => rjtest
[email] => 849929567895403#facebook.com
[id] => 849929567895403
)
)
)
[tags] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => inbox
)
[1] => stdClass Object
(
[name] => source:chat
)
)
)
)
[1] => Array
(
[message] => Happy New Year to all of you
[from] => stdClass Object
(
[name] => rjtest
[email] => 849929567895403#facebook.com
[id] => 849929567895403
)
[created_time] => 2015-12-31T10:37:47+0000
[id] => m_mid.1451558267514:4c017e5a1f8e997586
[to] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => rajesh Prasad Shah
[email] => 543634645764577#facebook.com
[id] => 543634645764577
)
)
)
[tags] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => inbox
)
[1] => stdClass Object
(
[name] => read
)
[2] => stdClass Object
(
[name] => sent
)
[3] => stdClass Object
(
[name] => source:web
)
)
)
)
[2] => Array
(
[message] => Happy New Year to all of you
[from] => stdClass Object
(
[name] => rjtest
[email] => 849929567895403#facebook.com
[id] => 849929567895403
)
[created_time] => 2015-12-31T09:44:49+0000
[id] => m_mid.1451555089630:1036078c7bcabeed78
[to] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => rajesh Prasad Shah
[email] => 543634645764577#facebook.com
[id] => 543634645764577
)
)
)
[tags] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => inbox
)
[1] => stdClass Object
(
[name] => read
)
[2] => stdClass Object
(
[name] => sent
)
[3] => stdClass Object
(
[name] => source:web
)
)
)
)
[3] => Array
(
[message] => Happy New Year to all of you
[from] => stdClass Object
(
[name] => rjtest
[email] => 849929567895403#facebook.com
[id] => 849929567895403
)
[created_time] => 2015-12-31T09:43:28+0000
[id] => m_mid.1451555008621:62c3365e35ededc773
[to] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => rajesh Prasad Shah
[email] => 543634645764577#facebook.com
[id] => 543634645764577
)
)
)
[tags] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => inbox
)
[1] => stdClass Object
(
[name] => read
)
[2] => stdClass Object
(
[name] => sent
)
[3] => stdClass Object
(
[name] => source:web
)
)
)
)
[4] => Array
(
[message] => Very Good
[from] => stdClass Object
(
[name] => rajesh Prasad Shah
[email] => 543634645764577#facebook.com
[id] => 543634645764577
)
[created_time] => 2015-09-01T09:13:34+0000
[id] => m_mid.1445648574720:c2746f8b55fff17369
[to] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => rjtest
[email] => 849929567895403#facebook.com
[id] => 849929567895403
)
)
)
[tags] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => inbox
)
[1] => stdClass Object
(
[name] => read
)
[2] => stdClass Object
(
[name] => source:chat
)
)
)
)
[5] => Array
(
[message] => Good
[from] => stdClass Object
(
[name] => rajesh Prasad Shah
[email] => 543634645764577#facebook.com
[id] => 543634645764577
)
[created_time] => 2015-09-01T09:13:32+0000
[id] => m_mid.1445648572476:316f06a4adc6d7dd57
[to] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => rjtest
[email] => 849929567895403#facebook.com
[id] => 849929567895403
)
)
)
[tags] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => inbox
)
[1] => stdClass Object
(
[name] => read
)
[2] => stdClass Object
(
[name] => source:chat
)
)
)
)
)
)
)
This is my php code from that i fetch data
foreach($data as $evaluatemessage){
//echo "<pre>";print_r($evaluatemessage); die;
$inbox=array();
$inbox['thread_id']=$evaluatemessage['threadId'];
foreach($evaluatemessage['messages'] as $messagedet){
//print_r($messagedet[0]['id']); die;
$inbox['smg_id']=$messagedet[$j]['id'];
$inbox['body']=$messagedet[$j]['message'];
$inbox['time']=$messagedet[$j]['created_time'];
//$inbox['msg_type']=$data;
//$inbox['user_id']=$data;
$inbox['From_name']=$messagedet[$j]['from']->name;
$inbox['To_name']=$messagedet[$j]['to']->data[0]->name;
$inbox['From_id']=$messagedet[$j]['from']->id;
$inbox['To_id']=$messagedet[$j]['to']->data[0]->id;
$inbox['tags']=$messagedet[$j]['tags']->data[1]->name;
//$inbox['Page_id']=$data;
//echo "<pre>";print_r($inbox); die;
$inbox['Page_id']='12345';
$this->facebook_inboxes->save($inbox);
$j++;
}
$i++;
}
when i hit with this code only a single threadid and single message contents are saved.but rest are not,i want every thread id with there all messages id and message body are saved row by row but unable to do that can anyone help me how i do that.
Write this code before Save function
$this->facebook_inboxes->create();
This is enbuild function of cake to store multiple row.
There is another messages array inside the messages array. So according to your array structure, your second loop will be like this :
foreach($evaluatemessage['messages']['messages'] as $messagedet){
//print_r($messagedet[0]['id']); die;
$inbox['smg_id']=$messagedet[$j]['id'];
$inbox['body']=$messagedet[$j]['message'];
$inbox['time']=$messagedet[$j]['created_time'];
.
.
.
}
and you are using only:
foreach($evaluatemessage['messages'] as $messagedet){}
here is the full code :
foreach($data as $evaluatemessage){
//echo "<pre>";print_r($evaluatemessage); die;
$inbox=array();
$inbox['thread_id']=$evaluatemessage['threadId'];
foreach($evaluatemessage['messages']['messages'] as $messagedet){
//print_r($messagedet[0]['id']); die;
$inbox['smg_id']=$messagedet[$j]['id'];
$inbox['body']=$messagedet[$j]['message'];
$inbox['time']=$messagedet[$j]['created_time'];
//$inbox['msg_type']=$data;
//$inbox['user_id']=$data;
$inbox['From_name']=$messagedet[$j]['from']->name;
$inbox['To_name']=$messagedet[$j]['to']->data[0]->name;
$inbox['From_id']=$messagedet[$j]['from']->id;
$inbox['To_id']=$messagedet[$j]['to']->data[0]->id;
$inbox['tags']=$messagedet[$j]['tags']->data[1]->name;
//$inbox['Page_id']=$data;
//echo "<pre>";print_r($inbox); die;
$inbox['Page_id']='12345';
$this->facebook_inboxes->save($inbox);
$j++;
}
$i++;
}

How to loop through a LinkedIn API response?

I am retrieving connections via LinkedIn's API.
This is a sample of the response. All I want to do is loop through this using PHP, accessing first and last names. Should be simple, but I am stumped. Thanks.
stdClass Object ( [_total] => 420 [values] => Array ( [0] => stdClass
Object ( [apiStandardProfileRequest] => stdClass Object ( [headers] =>
stdClass Object ( [_total] => 1 [values] => Array ( [0] => stdClass
Object ( [name] => x-li-auth-token [value] => name:AnUY ) ) ) [url] =>
http://api.linkedin.com/v1/people/7yw8k_sjqf ) [firstName] => John
[headline] => Finance Manager at ABC. [id] => 7yw8k_sjqf [industry] =>
Insurance [lastName] => Doe [location] => stdClass Object ( [country]
=> stdClass Object ( [code] => us ) [name] => Greater Atlanta Area ) [pictureUrl] =>
http://m.c.lnkd.licdn.com/mpr/mprx/0_73E_PMM3nzAIZLG03bHpPRo3qvCwZL_0DhYpPRogIxiFs7jUYPVRGNRGC1mGegyyqdihoC
[siteStandardProfileRequest] => stdClass Object ( [url] =>
http://www.linkedin.com/profile/view?id=2633833&authType=name&authToken=AnUY&trk=api*a189561*s197687*
) ) [1] => stdClass Object ( [apiStandardProfileRequest] => stdClass
Object ( [headers] => stdClass Object ( [_total] => 1 [values] =>
Array ( [0] => stdClass Object ( [name] => x-li-auth-token [value] =>
name:tpAo ) ) ) [url] => http://api.linkedin.com/v1/people/FW8obZbI8R
) [firstName] => Jeremy [headline] => Owner, BCD [id] => FW8obZbI8R
[industry] => Computer Software [lastName] => Doe [location] =>
stdClass Object ( [country] => stdClass Object ( [code] => us ) [name]
=> Greater Chicago Area ) [pictureUrl] => http://m.c.lnkd.licdn.com/mpr/mprx/0_Lw2NlImcd1e6LmQpL2mIleOMIcf9LeFpbu4IlexHq-R6V2ryWSpZ0HI6LM7rXeaEYf3Gt69H
[siteStandardProfileRequest] => stdClass Object ( [url] =>
http://www.linkedin.com/profile/view?id=6700159&authType=name&authToken=tpAo&trk=api*a189561*s197687*
) ) [2] => stdClass Object ( [apiStandardProfileRequest] => stdClass
Object ( [headers] => stdClass Object ( [_total] => 1 [values] =>
Array ( [0] => stdClass Object ( [name] => x-li-auth-token [value] =>
name:eETp ) ) ) [url] => http://api.linkedin.com/v1/people/a7-dvlvc7K
) [firstName] => Jane [headline] => Sr. Consultant System Integration
at AT&T [id] => a7-dvlvc7K [industry] => Telecommunications [lastName]
=> Doe [location] => stdClass Object ( [country] => stdClass Object ( [code] => us ) [name] => Greater New York City Area ) [pictureUrl] =>
http://m.c.lnkd.licdn.com/mpr/mprx/0_459zxGU8TBpTZjvisicExTgSSvZCsYXisCGExTR-t1ut5V1_N_6JA37p_Z4gJQqIK653RvmW
[siteStandardProfileRequest] => stdClass Object ( [url] =>
http://www.linkedin.com/profile/view?id=119413921&authType=name&authToken=eETp&trk=api*a189561*s197687*
) ) [3] => stdClass Object ( [apiStandardProfileRequest] => stdClass
Object ( [headers] => stdClass Object ( [_total] => 1 [values] =>
Array ( [0] => stdClass Object ( [name] => x-li-auth-token [value] =>
name:HjUG ) ) ) [url] => http://api.linkedin.com/v1/people/aie8Zpk9Gm
) [firstName] => John [headline] => Financial Information/Market Data
Professional [id] => aie8Zpk9Gm [industry] => Financial Services
[lastName] => Smith [location] => stdClass Object ( [country] =>
stdClass Object ( [code] => gb ) [name] => Glasgow, United Kingdom )
[pictureUrl] =>
http://m.c.lnkd.licdn.com/mpr/mprx/0_N0TLAL3BTlHZCVAbTNTAD03RCjngvsAbCIY-JfOxvg4JldxF9L7MShCp_Avp_ROhKS8LNY
[siteStandardProfileRequest] => stdClass Object ( [url] =>
http://www.linkedin.com/profile/view?id=1562499&authType=name&authToken=HjUG&trk=api*a189561*s197687*
) ) [4] => stdClass Object ( [apiStandardProfileRequest] => stdClass
Object ( [headers] => stdClass Object ( [_total] => 1 [values] =>
Array ( [0] => stdClass Object ( [name] => x-li-auth-token [value] =>
name:aZIS ) ) ) [url] => http://api.linkedin.com/v1/people/XTGgSuagWI
) [firstName] => Bob [headline] => CEO, Nycomed US at NMO [id] =>
XTGgSuagWI [industry] => Pharmaceuticals [lastName] => brown
[location] => stdClass Object ( [country] => stdClass Object ( [code]
=> us ) [name] => Greater New York City Area ) [pictureUrl] => http://m.c.lnkd.licdn.com/mpr/mprx/0_UciCehxmKngbM15je_qytdVG-zSbJrcje_92S7a-DlsFcA1m6iCGyXsjl-uwsq3ybXXbrd1L
[siteStandardProfileRequest] => stdClass Object ( [url] =>
What you have as the response is an stdclass which will have all of its properties public.
This is how you iterate it:
$response = $this->getResponseFromLinkedIn();
foreach ($response->values as $value) {
$firstName = $value->firstName;
$lastName = $value->lastName;
// do something with the extracted values!
}

PHP - Objects and Arrays - How to access an stdClass object "name" "value" pair inside an array?

I would like to know how to return a value of an object with a name value pair that's inside an array. I've been trying all sorts of methods and frankly I realized I may be way over my head on this. I'd like some assistance trying to get the AirportsInformation_DataExtension value inside the property array.
stdClass Object
(
[OverallStatus] => OK
[RequestID] => 19e41b46-df68-47ba-8858-d728f3a92036
[Results] => stdClass Object
(
[PartnerKey] =>
[ObjectID] =>
[Type] => DataExtensionObject
[Properties] => stdClass Object
(
[Property] => Array
(
[0] => stdClass Object
(
[Name] => CampaignName
[Value] => 20130107_FlightDealsHotelPricePoints
)
[1] => stdClass Object
(
[Name] => StartDate
[Value] => 1/7/2013 12:00:00 AM
)
[2] => stdClass Object
(
[Name] => EndDate
[Value] => 1/15/2013 5:59:59 AM
)
[3] => stdClass Object
(
[Name] => CampaignType
[Value] => FlightDeals
)
[4] => stdClass Object
(
[Name] => LandingPage_ExpireDate
[Value] => 1/15/2013 5:59:59 AM
)
[5] => stdClass Object
(
[Name] => LandingPage_AutoRedirectOnExpire
[Value] => True
)
[6] => stdClass Object
(
[Name] => LandingPage_ExpireTargetURL
[Value] => test
)
[7] => stdClass Object
(
[Name] => BookByDate
[Value] => 1/22/2013 12:00:00 AM
)
[8] => stdClass Object
(
[Name] => TravelStartDate
[Value] =>
)
[9] => stdClass Object
(
[Name] => TravelEndDate
[Value] =>
)
[10] => stdClass Object
(
[Name] => FlightDeals_DataExtension
[Value] => 20130107_DestinationFlightDeals
)
[11] => stdClass Object
(
[Name] => FlightDeals_SortOrder_DataExtension
[Value] => FlightDeals_DestinationSortOrder
)
[12] => stdClass Object
(
[Name] => HotelDeals_DataExtension
[Value] => 20130107_FlightDealsHotelPricePoints
)
[13] => stdClass Object
(
[Name] => HotelDeals_All_DataExtension
[Value] => 20130107_HotelPackageDeals_ALL
)
[14] => stdClass Object
(
[Name] => HotelInformation_DataExtension
[Value] => EmailHotelInformation
)
[15] => stdClass Object
(
[Name] => AirportsInformation_DataExtension
[Value] => Airports
)
[16] => stdClass Object
(
[Name] => RoutesInformation_DataExtension
[Value] => Routes
)
[17] => stdClass Object
(
[Name] => DFP_DataExtension
[Value] => ET_DestinationIframeSrc
)
[18] => stdClass Object
(
[Name] => DeepLinkConnectorURL
[Value] => http://www.somewebsite/BookingConnector.html?mode=run
)
[19] => stdClass Object
(
[Name] => DefaultDestinationScenery
[Value] => LAS
)
[20] => stdClass Object
(
[Name] => DefaultHomeAirportCode
[Value] =>
)
[21] => stdClass Object
(
[Name] => FailSafeHomeAiportCode
[Value] =>
)
[22] => stdClass Object
(
[Name] => DFP_Campaign_Banner
[Value] => True
)
[23] => stdClass Object
(
[Name] => EmailID
[Value] => 44388
)
)
)
)
)
Using a foreach loop I was able to print out all lines with name/value sets
foreach ($results->Results->Properties->Property as $CurrentProp){
print('<br>');
print('Name: '.$CurrentProp->Name. ' Value: '.$CurrentProp->Value.'<br>');
};
Sadly I can't get passed that. I just need to retrieve the value. Thanks in advance.
In order to get the value, you could loop over them and test for the name matching AirportsInformation_DataExtension:
foreach ($results->Results->Properties->Property as $CurrentProp){
if($CurrentProp->Name == 'AirportsInformation_DataExtension')
{
echo 'The value is: ' . $CurrentProp->Value;
}
}
If you would need to be able to get all the values based on their name, it could be useful to turn it in to an associative array, like this
$results->Results->Properties->PropertyArray = array();
foreach($results->Results->Properties->Property as $arrCurrentProperty) {
$results->Results->Properties->PropertyArray[$arrCurrentProperty->Name] = $arrCurrentProperty->Value;
};
Then you can get the values later by indexing them directly, i.e.
echo 'The value is: ' . $results->Results->Properties->PropertyArray['AirportsInformation_DataExtension'];
Use the above solution by MrCode or, simply just use $results->Results->Properties->Property[15]->Value if the index of AirportsInformation_DataExtension is always 15. Since an array is an ordered list, it is very probably that the index does not change unless some of the items are removed/added from the array/
foreach ($results->Results->Properties->Property as $CurrentProp){
$tempArr[$CurrentProp->Name] = $CurrentProp->Value;
}
echo $tempArr['AirportsInformation_DataExtension'];
By this you can access any other key of that object.

Categories