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);
}
}
}
Related
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>';
}
}
I got a response like this now i want to print
[products] how to print
i try this print_r($response['products']);
My Response
stdClass Object
(
[users] => Array
(
[0] => stdClass Object
(
[username] => admin
[firstName] => admin
[lastName] => admin
[products] => Array
(
[0] => stdClass Object
(
[name] => 1
[url] => mytest.html
)
[1] => stdClass Object
(
[name] => 2
[url] => mytest.html
)
[2] => stdClass Object
(
[name] => 3
[url] => mytest.html
)
[3] => stdClass Object
(
[name] => 4
[url] => mytest.html
)
[4] => stdClass Object
(
[name] => 5
[url] => mytest.html
)
[5] => stdClass Object
(
[name] => 6
[url] => mytest.html
)
[6] => stdClass Object
(
[name] => 7
[url] => mytest.html
)
)
)
)
[error] =>
[errorMessage] =>
)
Try This one
print_r($response->users['0']->products);
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
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 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
)