PHP Loop Object member manipulation - php

I have a multi dimensional object result set to which I want to loop through to change a value of an inner object member to the value of an outer object member. When the loop finishes executing, even though the values of each outer object member is different, the values of the inner object member are all the same value which is the value of the final outer object member.
foreach ($user as $key => &$value) {
$user[$key]->user->company = $value->Company;
}
When I dump the $user array which contains the objects I get This as my result:
Array
(
[0] => common\models\UserRoleMapping Object
(
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[User_Role_Mapping_Id] => 9b55d3a7-8de6-11e5-ae99-60a44ce86902
[User_Id] => 9b432c2c-8de6-11e5-ae99-60a44ce86902
[User_Role_Id] => 49c82d92-31d0-11e4-8309-60a44ce86902
[Status] => 8332b8d7-1990-11e4-8876-60a44ce86902
[Company] => 79538f95-7e25-11e5-b514-60a44ce86902
)
[_related:yii\db\BaseActiveRecord:private] => Array
(
[user] => common\models\User Object
(
[company] => 0e7505fc-7961-11e5-a563-60a44ce86902
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[User_Id] => 9b432c2c-8de6-11e5-ae99-60a44ce86902
[Username] => andre#hotmail.com
[Password] => fbb8f9f42cb0532f30759d9e4fa3e2fc90d714321ce11fdc42f5c9b23756979c6166e674e421b8580ab53a0e75e9d2758f7b8bbe70a33a5ad0cdc25b553e08b4
[Login_Status] => Offline
[Failed_Login_Attempts] => 0
[Verification] =>
[Salt] => dd7c8a297a8d34ba5dc7cd4b45a1e593
[Last_Reset] => 0000-00-00 00:00:00
[Status] => 8332b8d7-1990-11e4-8876-60a44ce86902
)
)
)
)
[1] => common\models\UserRoleMapping Object
(
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[User_Role_Mapping_Id] => fdb962ef-8de6-11e5-ae99-60a44ce86902
[User_Id] => 9b432c2c-8de6-11e5-ae99-60a44ce86902
[User_Role_Id] => 49c82d92-31d0-11e4-8309-60a44ce86902
[Status] => 8332b8d7-1990-11e4-8876-60a44ce86902
[Company] => 0e7505fc-7961-11e5-a563-60a44ce86902
)
[_related:yii\db\BaseActiveRecord:private] => Array
(
[user] => common\models\User Object
(
[company] => 0e7505fc-7961-11e5-a563-60a44ce86902
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[User_Id] => 9b432c2c-8de6-11e5-ae99-60a44ce86902
[Username] => andre#hotmail.com
[Password] => fbb8f9f42cb0532f30759d9e4fa3e2fc90d714321ce11fdc42f5c9b23756979c6166e674e421b8580ab53a0e75e9d2758f7b8bbe70a33a5ad0cdc25b553e08b4
[Login_Status] => Offline
[Failed_Login_Attempts] => 0
[Verification] =>
[Salt] => dd7c8a297a8d34ba5dc7cd4b45a1e593
[Last_Reset] => 0000-00-00 00:00:00
[Status] => 8332b8d7-1990-11e4-8876-60a44ce86902
)
)
)
)
)
As you can see in the dumped values, both inner user objects have the same value for their company member which the value Found in the final Outer Object Company member.
I am seeking assistance with how I can get each inner user object to have their respective outer object company values.

Related

Push array object into another array object in php

I'm getting an error while pushing one object into another object. But the 2nd object is an array and inside an array there is an object. How can I fix this cause I want to add that into my object
My object just like this
I want to add the the Object2 into Object1
Objet1
stdClass Object
(
[id_laporan_pemeriksa] => 5
[no_pkpt] => SNE
[tgl_pkpt] => 2010
[no_penugasan] => ST-4000/PW25/2/2017
[tgl_penugasan] => 2017-08-09
[judul_laporan] => Masukkan Kode disini
[no_laporan] => LBINA-9000/PW25/2/2017
[tgl_laporan] => 2017-08-01
[tahun_anggaran_penugasan] => 2009
[nilai_anggaran_penugasan] => 10000000
[realisasi_anggaran_penugasan] => 100000000
[jenis_anggaran_penugasan] => Utang
[sumber_laporan] => Inspektorat Maluku
[nama_sumber_penugasan] => PKPT
[nama_ketua_tim] => Abdul Rofiek, Ak.
[nama_pengendali_teknis] => Alfian Massagony, S.E.
[nama_unit_penugasan] => Irban Wil. I
[nama_penugasan] => Penjaminan
[nama_sub_penugasan] => Audit
[id_s_sub_penugasan] => 010105
[nama_s_sub_penugasan] => Audit atas hal-hal lain di bidang kepegawaian.
)
Object2
stdClass Object
(
[id] => 3
[data_sebab] => Array
(
[0] => stdClass Object
(
[id] => 4
[data_rekomendasi] => Array
(
[0] => stdClass Object
(
[id] => 4
[data_tindak_lanjut] => Array
(
[0] => stdClass Object
(
[id] => 9
[tgl_tindak_lanjut] => 0000-00-00
)
)
)
[1] => stdClass Object
(
[id] => 5
[id_rekomendasi] =>
[data_tindak_lanjut] => Array
(
[0] => stdClass Object
(
[id] => 10
[id_tindak_lanjut] =>
[tgl_tindak_lanjut] => 0000-00-00
)
[1] => stdClass Object
(
[id] => 11
[id_tindak_lanjut] =>
[tgl_tindak_lanjut] => 0000-00-00
)
)
)
)
)
)
)
I have tried
$Object1['data']->$Object2;
But i got an error
Cannot use object of type stdClass as array
The syntax of adding $Object2 as a property of $Object1 is:
$Object1->Object2 = $Object2;
Or:
$Object1->{'Object2'} = $Object2;
It should be:
$Object1->data = $Object2; // it will create data element with obj2 as value
As the objects are objects and not arrays, using:
$Object1['data']->$Object2;
wont work. However doing the following will work:
$Object1->data = $Object2;

How to get value out of this...?

Can someone explain me how to get data out of this...like if I just want subject, description..etc...
stdClass Object
(
[tickets] => Array
(
[0] => stdClass Object
(
[url] => https://codemymobilecom.zendesk.com/api/v2/tickets/1.json
[id] => 1
[external_id] =>
[via] => stdClass Object
(
[channel] => sample_ticket
[source] => stdClass Object
(
[from] => stdClass Object
(
)
[to] => stdClass Object
(
)
[rel] =>
)
)
[created_at] => 2015-04-22T08:30:29Z
[updated_at] => 2015-05-19T06:01:22Z
[type] => incident
[subject] => This is a sample ticket requested and submitted by you
[raw_subject] => This is a sample ticket requested and submitted by you
[description] => This is the first comment. Feel free to delete this sample ticket.
[priority] => high
[status] => closed
[recipient] =>
[requester_id] => 794599791
[submitter_id] => 794599791
[assignee_id] => 794599791
[organization_id] => 39742491
[group_id] => 24344491
[collaborator_ids] => Array
(
)
[forum_topic_id] =>
[problem_id] =>
[has_incidents] =>
[due_at] =>
[tags] => Array
(
[0] => sample
[1] => zendesk
)
[custom_fields] => Array
(
)
[satisfaction_rating] =>
[sharing_agreement_ids] => Array
(
)
[fields] => Array
(
)
[followup_ids] => Array
(
)
[brand_id] => 565681
)
[1] => stdClass Object
(
[url] => https://codemymobilecom.zendesk.com/api/v2/tickets/10.json
[id] => 10 //multiple object like [0]...
Thanks...Any help would be great..
When you need to access to array's key, use []. When you have object, use ->.
echo $obj->tickets[0]->subject; // returns first subject
echo $obj->tickets[0]->description; // returns first description
You can put it into foreach loop, of course to gain all subjects, etc.
It's STD object so use properties
$obj->tickets[0]->subject
$obj->tickets[0]->description
You can obviously loop tickets
foreach($obj->tickets as $ticket)
{
echo $ticket->subject;
echo $ticket->description
}
this is an std object.to get subject and description follow this
$obj->tickets[0]->subject;
$obj->tickets[0]->description;
if you feel better in array just make it array using this code
$array = get_object_vars($obj);

How can I merge or search an object?

Here's my issue:
I have an object filled with arrays that look like this.
[376339] => Array
(
[0] => 1f422730-f54b-4e4d-9289-10258ce74446
[1] => 60dc4646-06ce-44d0-abe9-ee371847f4df
)
I need to search another object to find objects with the matching IDs, like below. Is there a way of doing this without a foreach? There are SEVERAL and I would like to not have to loop over the entire object every time.
stdClass Object
(
[id] => 1f422730-f54b-4e4d-9289-10258ce74446
[percentage] => 32
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 59826
[destination_id] => 59826
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxx
)
)
)
stdClass Object
(
[id] => 60dc4646-06ce-44d0-abe9-ee371847f4df
[percentage] => 68
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 60046
[destination_id] => 60046
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxxx
)
)
)
I need it to end up looking like this.
[376339] => Array
(
[0] => Array
(
[id] => 1f422730-f54b-4e4d-9289-10258ce74446
[percentage] => 32
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 59826
[destination_id] => 59826
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxx
)
)
)
[1] => Array
(
[id] => 60dc4646-06ce-44d0-abe9-ee371847f4df
[percentage] => 68
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 60046
[destination_id] => 60046
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxxx
)
)
)
)
I'm not sure if this makes any sense, that's why I had my two inital outputs I need to have merged into one somehow. This is all coming from one huge json object and I'm just using json_decode($jsonStuff) to decode it.
Would this be easier if I added true in the decode function? If I could just search for it like I could in python, that would be neat. But as it is, I'm at a loss as to how to get the output I need.
Note: Input json CANNOT be changed, I have no affiliation with the people that created it.
First loop over your input array and create an array with the key as the id
$input = json_decode($json_input);
$output = array();
foreach($input as $obj){
$output[$obj->id] = $obj;
}
then you can build your other array by searching the id on the array key
$massive_search_array = array(376339 => array
(
0 => 1f422730-f54b-4e4d-9289-10258ce74446,
1 => 60dc4646-06ce-44d0-abe9-ee371847f4df
)
);
$final_output = array();
foreach($massive_search_array as $index => $searches){
foreach($searches as $search){
if(isset($output[$search])){
$final_output[$index][] = $output[$search];
}
}
}

Return value when searching for key in array PHP

I tried to get an answer for this in other posts with no luck, hope someone can help me here, i have a multidimensional array:
Array (
[0] => stdClass Object (
[affectsVersions] => Array ( )
[assignee] => hmontes
[attachmentNames] => Array ( )
[components] => Array ( )
[created] => 2012-08-15T05:31:26.000Z
[customFieldValues] => Array (
[0] => stdClass Object (
[customfieldId] => customfield_10201
[key] => [values] => Array (
[0] => 123456
)
)
[1] => stdClass Object (
[customfieldId] => customfield_10004
[key] => [values] => Array (
[0] => 30
)
)
)
[description] => [duedate] => [environment] => [fixVersions] => Array ( )
[id] => 10228
[key] => NTP-29
[priority] => 3
[project] => NTP
[reporter] => hmontes
[resolution] => [status] => 1
[summary] => case 123456
[type] => 3
[updated] => 2012-08-15T05:31:26.000Z
[votes] => 0
)
)
this is what i get when i do a print_r with the array variable, i need to search and get the value from [key] that would be in this case NTP-29 and keep it in a variable as string.
You can get the value of an array by the key using $array['keyName'];
But, for you it looks like you just need to go deeper $array[0]['key'];
Both arrays values and properties of objects can be accessed using associative array syntax. To get the value of the key property in your object within the array you'd do the following, assuming $array is a variable containing a reference to your array:
$key = $array[0]['key']; // accesses NTP-29 in this case.
Here's another way to access the same property, using object property-access syntax:
$key = $array[0]->key; // also accesses NTP-29.

segregating php object variables

I have some XML that is being returned to be as an object, like this:
SwitchvoxResponse Object
(
[apiStatus:private] => success
[apiErrors:private] => Array
(
)
[apiResult:private] => Array
(
[calls] => Array
(
[page_number] => 1
[total_pages] => 1
[items_per_page] => 50
[total_items] => 1
[call] => Array
(
[0] => Array
(
[id] => 14301
[origination] => outgoing
[start_time] => 2011-06-17 13:40:58
[from] => CALLER_NAME <4485>
[from_account_id] => 1120
[from_name] => CALLER_NAME
[from_number] => 4485
[to] => CALLEE_NAME <6534>
[to_account_id] => 1101
[to_name] => CALLEE_NAME
[to_number] => 6534
[total_duration] => 47
[talk_duration] => 43
[events] => Array
(
[event] => Array
(
[0] => Array
(
[start_time] => 2011-06-17 13:40:58
[type] => OUTGOING
[display] => Dialed number (6534)
)
[1] => Array
(
[start_time] => 2011-06-17 13:40:58
[type] => INTERNAL
[display] => Rang CALLEE_NAME <6534>
)
[2] => Array
(
[start_time] => 2011-06-17 13:41:02
[type] => TALKING
[display] => Talked to CALLEE_NAME <6534> for 43 seconds
)
[3] => Array
(
[start_time] => 2011-06-17 13:41:45
[type] => HANGUP
[display] => Call was hung up by CALLER_NAME <4485>
)
)
)
)
)
)
)
)
How do I pull out the values of these variables?
The results can be accessed via the SwitchvoxResponse::getResult() method. Given $object is the SwitchvoxResponse object quoted in the question, the example below loops over each call and prints the from values.
$result = $object->getResult();
foreach ($result['calls']['call'] as $call) {
echo $call['from'];
}
Similarly, the response status is fetched via $object->getResponseStatus() and any errors via $object->getErrors().
The response statuses can be one of SV_RESPONSE_SUCCESS, SV_RESPONSE_FAULT or SV_RESPONSE_FAILED.
Edit re. comments
To get the items for the first call only, simply do:
$result = $object->getResult();
$call = $result['calls']['call'][0];
// And access the values like
echo $call['from_name'];
All three properties are marked private, so they would have to be obtained through accessor methods marked public

Categories