Reaching elements of an object - php

How can i reach elements of this object with php? I have tried $obj[0]->trends to reach inner array but php gives me error Fatal error: Cannot use object of type stdClass as array in /.....file.php on line 97
stdClass Object
(
[0] => stdClass Object
(
[trends] => Array
(
[0] => stdClass Object
(
[name] => IWorkAsHardAs
[query] => IWorkAsHardAs
[events] =>
)
[1] => stdClass Object
(
[name] => ProtectOzoneLayer
[query] => ProtectOzoneLayer
[events] =>
)
)
[as_of] => 2013-09-16T13:06:47Z
[created_at] => 2013-09-16T12:44:57Z
[locations] => Array
(
[0] => stdClass Object
(
[name] => Jaipur
[woeid] => 2295401
)
)
)
[httpstatus] => 200
)

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>';
}
}

PHP - Dealing with a Indexed Arrays

I have an array of arrays that I'm finding trouble looping with foreach and getting accessing the data.
They array key has dates, then the array inside this has badly ordered indexes.
array(
[2016-11-03T07:00:00] => Array
(
[0] => stdClass Object
(
[Job] => stdClass Object
(
[ID] => 87126387136
[Foo] => stdClass Object
(
[ID] => 0
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
)
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
[Premises] => stdClass Object
(
[Location] => stdClass Object
(
[Metric] => stdClass Object
(
[Latitude] => 51.3453534534
[Longitude] => -0.417308
)
)
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
)
[ScheduledStart] => 2016-11-03T07:00:00
)
)
[6] => stdClass Object
(
[Job] => stdClass Object
(
[ID] => 234923749
[Foo] => stdClass Object
(
[ID] => 0
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
)
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
[Premises] => stdClass Object
(
[Location] => stdClass Object
(
[Metric] => stdClass Object
(
[Latitude] => 51.34535435345
[Longitude] => -0.417308
)
)
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
)
[ScheduledStart] => 2016-11-03T07:00:00
)
)
)
[2016-11-24T07:00:00] => Array
(
[3] => stdClass Object
(
[Job] => stdClass Object
(
[ID] => 3749823749
[Foo] => stdClass Object
(
[ID] => 0
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
)
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
[Premises] => stdClass Object
(
[Location] => stdClass Object
(
[Metric] => stdClass Object
(
[Latitude] => 51.345345435
[Longitude] => -0.417308
)
)
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
)
[ScheduledStart] => 2016-11-24T07:00:00
)
)
[5] => stdClass Object
(
[Job] => stdClass Object
(
[ID] => 4792874923874
[Foo] => stdClass Object
(
[ID] => 0
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
)
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
[Premises] => stdClass Object
(
[Location] => stdClass Object
(
[Metric] => stdClass Object
(
[Latitude] => 51.3453453444
[Longitude] => -0.417308
)
)
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
)
[ScheduledStart] => 2016-11-24T07:00:00
)
)
[9] => stdClass Object
(
[Job] => stdClass Object
(
[ID] => 9943386
[Foo] => stdClass Object
(
[ID] => 0
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
)
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
[Premises] => stdClass Object
(
[Location] => stdClass Object
(
[Metric] => stdClass Object
(
[Latitude] => 51.34534534534
[Longitude] => -0.417308
)
)
[RecordStamp] => stdClass Object
(
[AddedBy] => dbo
)
)
[ScheduledStart] => 2016-11-24T07:00:00
)
)
)
)
Is it possible to loop through this array using foreach in it's current state?
I'm open to suggestion of manipulating the array if for a more efficient solution.
There are two layers of arrays - the first with dates as indices, the second with numbers as indices. Values of the second arrays are nested objects.
foreach ($arr as $date => $v) {
echo "date: ".$date."<br>";
foreach ($v as $number => $job) {
echo "number: ".$number."<br>";
echo $job->ID."<br>";
echo $job->Foo->ID."<br>";
echo $job->Foo->RecordStamp->AddedBy."<br>";
echo $job->Premises->Location->Metric->Latitude."<br>";
echo $job->ScheduledStart."<br>";
}
}

How to Print response in csv file using PHP

I have response array in variable $listing array .Here is $listing Value:
Array
(
[0] =>
[1] => stdClass Object
(
[status] => failure
[response] => Array
(
[0] => stdClass Object
(
[ProId] => 1
[status] => failure
[errors] => Array
(
[0] => stdClass Object
(
[errorCode] => CONTENT_NOT_FOUND
[message] => Content not found.
[attributeName] =>
)
)
)
[1] => stdClass Object
(
[ProId] => 2
[status] => failure
[errors] => Array
(
[0] => stdClass Object
(
[errorCode] => CONTENT_NOT_FOUND
[message] => Content not found.
[attributeName] =>
)
)
)
[2] => stdClass Object
(
[ProId] => 3
[status] => updated
[errors] => Array
(
)
)
[3] => stdClass Object
(
[ProId] => 4
[status] => updated
[errors] => Array
(
)
)
[4] => stdClass Object
(
[ProId] => 5
[status] => updated
[errors] => Array
(
)
)
[5] => stdClass Object
(
[ProId] => 6
[status] => updated
[errors] => Array
(
)
)
[6] => stdClass Object
(
[ProId] => 7
[status] => updated
[errors] => Array
(
)
)
[7] => stdClass Object
(
[ProId] => 8
[status] => updated
[errors] => Array
(
)
)
[8] => stdClass Object
(
[ProId] => 9
[status] => updated
[errors] => Array
(
)
)
[9] => stdClass Object
(
[ProId] => 10
[status] => updated
[errors] => Array
(
)
)
)
)
[2] => stdClass Object
(
[status] => failure
[response] => Array
(
[0] => stdClass Object
(
[ProId] => 11
[status] => failure
[errors] => Array
(
[0] => stdClass Object
(
[errorCode] => CONTENT_NOT_FOUND
[message] => Content not found.
[attributeName] =>
)
)
)
[1] => stdClass Object
(
[ProId] => 12
[status] => failure
[errors] => Array
(
[0] => stdClass Object
(
[errorCode] => CONTENT_NOT_FOUND
[message] => Content not found.
[attributeName] =>
)
)
)
[2] => stdClass Object
(
[ProId] => 13
[status] => failure
[errors] => Array
(
[0] => stdClass Object
(
[errorCode] => CONTENT_NOT_FOUND
[message] => Content not found.
[attributeName] =>
)
)
)
[3] => stdClass Object
(
[ProId] => 14
[status] => updated
[errors] => Array
(
)
)
[4] => stdClass Object
(
[ProId] => 15
[status] => updated
[errors] => Array
(
)
)
[5] => stdClass Object
(
[ProId] => 16
[status] => updated
[errors] => Array
(
)
)
[6] => stdClass Object
(
[ProId] => 17
[status] => updated
[errors] => Array
(
)
)
[7] => stdClass Object
(
[ProId] => 18
[status] => updated
[errors] => Array
(
)
)
[8] => stdClass Object
(
[ProId] => 19
[status] => updated
[errors] => Array
(
)
)
[9] => stdClass Object
(
[ProId] => 20
[status] => updated
[errors] => Array
(
)
)
)
)
)
I want to create one csv file with above response in below format .Please see attached image
For achieving this result i am using this code:
$fp = fopen('output.csv',"w");
$rowcounts=0;
$resultResponse=$listing->response;
while($rowcounts<$inputfielscount) {
$result=$resultResponse[$rowcounts];
$resultProId=$result->ProId;
$resultStatus=$result->status;
$resultErrors=$result->errors;
$errorMsgArray=$resultErrors[0];
$ErrorsMessage=$errorMsgArray->message;
$ErrorCode=$errorMsgArray->errorCode;
if($resultStatus=='failure'){
$list = array ($resultProId,$resultStatus,$ErrorCode,$ErrorsMessage);
}else {
$list = array ($resultProId,$resultStatus,"successfully");
}
fputcsv($fp,$list);
$rowcounts++;
}
$listing is an response array what i already mansioned at first.
I am not getting the desired result in csv file.If anyone has solution please share.
This I think will shove it into an flat multi-diamential array, not too much trouble to then get it into a csv.
foreach($array as $key => $value) {
if(isset($array[$key]) && count($array[$key]) > 0) {
foreach($array[$key] as $inner) {
$error = $inner['errors'][0];
$new_array[] = array($inner->ProId,$inner->status,$error->errorCode,$error->message);
}
}
}

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 can I parse this result?

Array (
[0] => stdClass Object (
[name] => query1
[fql_result_set] => Array ( [0] => stdClass Object ( [fan_count] => 226 ) ) )
[1] => stdClass Object (
[name] => query2
[fql_result_set] => Array ( [0] => stdClass Object ( [fan_count] => 247 ) ) )
[2] => stdClass Object (
[name] => query3
[fql_result_set] => Array ( [0] => stdClass Object ( [fan_count] => 580 ) ) )
[3] => stdClass Object (
[name] => query4
[fql_result_set] => Array ( [0] => stdClass Object ( [fan_count] => 10 ) ) )
[4] => stdClass Object (
[name] => query5
[fql_result_set] => Array ( [0] => stdClass Object ( [fan_count] => 508 ) ) )
)
How I can parse this result ?
I searched, but I didn't found out how I could do it.
Assuming your array variable is called $data:
foreach ($data as $object) {
$name = $object->name;
$result_set = $object->fql_result_set;
$fan_count = $result_set[0]->fan_count;
// Do what you need to with the data here
}
Hope that helps

Categories