Weird save to array in foreach in php - php

Something strange happens during saving into an array inside a foreach.
I'm getting unexpected results so I make this part of code for debuging.
$search_case = new stdClass();
$same_case = array ();
print_r($families);
foreach ($families as $key => $family) {
echo "<br/><br/>";
$same_case['aaaaa'.$key] = $this->search_benef->search_case($family, $search_case);
print_r($same_case);
}
The problem is that when I get function's results for $key = 1 it also change the data in table for $key = 0. To chek that I created aaaaa0 and aaaaa1 names for my table.
Bellow is print_r results:
First print_r I have 2 objects, IBRAHIM and MOHAMMAD
stdClass Object ( [0] => stdClass Object ( [focal] => stdClass Object ( [name] => RENAS [surname] => IBRAHIM [case_number] => 36343 ) [case] => 36343 ) [1] => stdClass Object ( [focal] => stdClass Object ( [name] => AZAD [surname] => MOHAMMAD [case_number] => 54944 ) [case] => 54944 ) )
Second print_r inside foreach loop, in the first loop I get results for IBRAHIM and in the second loop Ι get results for MOHAMMAD, but it removes results for IBRAHIM in aaaaa0.
Array ( [aaaaa0] => stdClass Object ( [old] => stdClass Object ( [hotel] => stdClass Object ( [focal] => stdClass Object ( [id] => 26 [name] => RENAS [surname] => IBRAHIM ) ) ) ) )
Array ( [aaaaa0] => stdClass Object ( [old] => stdClass Object ( [hotel] => stdClass Object ( [focal] => stdClass Object ( [id] => 22 [name] => AZAD [surname] => MOHAMMAD ) ) [athens] => stdClass Object ( [focal] => stdClass Object ( [id] => 24 [name] => AZAD [surname] => MOHAMMAD ) ) ) ) [aaaaa1] => stdClass Object ( [old] => stdClass Object ( [hotel] => stdClass Object ( [focal] => stdClass Object ( [id] => 22 [name] => AZAD [surname] => MOHAMMAD ) ) [athens] => stdClass Object ( [focal] => stdClass Object ( [id] => 24 [name] => AZAD [surname] => MOHAMMAD ) ) ) ) )
Can you explain why this happens? Which is the error?
-- EDIT --
I change my code and I remove 'search_case' function, but I face the same problem. The content changes. Below I paste my new code without the function.
$aaaaa = array ();
foreach ($families as $key => $family) {
echo "<br/><br/>Family->Case Number: " . $family->case;
$db_lists = array('athens' => 'athensDB', 'hotel' => 'hotelDB');
foreach ($db_lists as $key => $db_list) {
$remoteDB = $this->ci->load->database($key, TRUE);
$query = 'SELECT tb.id as id, name, surname FROM beneficiaries tb
left join _map_family_status tmfs ON tmfs.id=tb.family_status
where asylum_case = ' . $family->case . ' AND family_status = 1 GROUP BY asylum_case';
$focal = $remoteDB->query($query)->row();
if ($focal) {
$search_case->old->$key->focal = $focal;
}
}
echo '<br/> '.$family->case.' <br/> ';
$aaaaa[$family->case] = $search_case;
echo "<pre>";
print_r($aaaaa);
echo "</pre>";
}
return $aaaaa;
But the results are dissapointing. Array[36343] content changes with the second loop's content. Below I paste "print_r" results for the two loops.
Family->Case Number: 36343
Array (
[36343] => stdClass Object (
[old] => stdClass Object (
[hotel] => stdClass Object (
[focal] => stdClass Object (
[id] => 26
[name] => RENAS
[surname] => IBRAHIM
)
)
)
)
)
Family->Case Number: 54944
Array (
[36343] => stdClass Object (
[old] => stdClass Object (
[hotel] => stdClass Object (
[focal] => stdClass Object (
[id] => 22
[name] => AZAD
[surname] => MOHAMMAD
)
)
[athens] => stdClass Object (
[focal] => stdClass Object (
[id] => 24
[name] => AZAD
[surname] => MOHAMMAD
)
)
)
)
[54944] => stdClass Object (
[old] => stdClass Object (
[hotel] => stdClass Object (
[focal] => stdClass Object (
[id] => 22
[name] => AZAD
[surname] => MOHAMMAD
)
)
[athens] => stdClass Object (
[focal] => stdClass Object (
[id] => 24
[name] => AZAD
[surname] => MOHAMMAD
)
)
)
)
)

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

Iterate through stdClass

How on Earth do I get the ids, and names from this array (possibly by using a ForEach construct)?
stdClass Object (
[data] => Array (
[0] => stdClass Object ( [name] => Corinne Culbard [id] => 5005561918 )
[1] => stdClass Object ( [name] => Tom West [id] => 5034297044 )
[2] => stdClass Object ( [name] => Sabri Tasci [id] => 5057373639 )
Is this not working?
foreach ($object->data as $item) {
$name = $item->name;
$id = $item->id;
}

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