Get values from STDClass Object and create new array - php

I need to pull the ShipQty from each of the following STDClass Objects and insert it into a new array.
Array ( [0] => stdClass Object ( [PRODUCTID] => 2 [ORDERID] => 7 [SHIPQTY] => 3 [STATUS] => 1 [SHIPDATE] => 2013-12-18 ) [1] => stdClass Object ( [PRODUCTID] => 2 [ORDERID] => 5 [SHIPQTY] => 2 [STATUS] => 1 [SHIPDATE] => 2014-01-02 ) [2] => stdClass Object ( [PRODUCTID] => 2 [ORDERID] => 2 [SHIPQTY] => 5 [STATUS] => 1 [SHIPDATE] => 2014-01-08 ) [3] => stdClass Object ( [PRODUCTID] => 2 [ORDERID] => 1 [SHIPQTY] => 2 [STATUS] => 1 [SHIPDATE] => 2014-01-16 ) )
I want the following: array(3,2,5,2)
Thank you in advance!

try this (assuming your array is named $myArr):
$result = array();
foreach ($myArr as $obj) {
$result[] = $obj->SHIPQTY;
}
Hope this helps.

Related

Merging two objects based on key value

I have two objects from an API I am trying to merge, one is a list of available subscription packages and one is a list of the current user's active subscription's and the corresponding status.
The first object looks like this:
This\Collection Object
(
[object] => list
[data] => Array
(
[0] => This\Product Object
(
[id] => 444
[name] => Test Plan 4
)
[1] => This\Product Object
(
[id] => 333
[name] => Test Plan 3
)
[2] => This\Product Object
(
[id] => 222
[name] => Test Plan 2
)
[3] => This\Product Object
(
[id] => 111
[name] => Test Plan 1
)
)
[has_more] =>
)
This second object looks like this:
Array
(
[0] => This\Subscription Object
(
[id] => sub_xxx
[current_period_end] => 1670307654
[current_period_start] => 1667715654
[plan] => This\Plan Object
(
[currency] => usd
[product] => 111
)
[start_date] => 1667715654
[status] => active
)
[1] => This\Subscription Object
(
[id] => sub_xxx
[current_period_end] => 1770507654
[current_period_start] => 1767715454
[plan] => This\Plan Object
(
[currency] => usd
[product] => 333
)
[start_date] => 1767715654
[status] => active
)
)
I want to combine the second object with the first object if subscription->plan->product matches product->id so it would look something like this:
This\Collection Object
(
[object] => list
[data] => Array
(
[0] => This\Product Object
(
[id] => 444
[name] => Test Plan 4
)
[1] => This\Product Object
(
[id] => 333
[name] => Test Plan 3
[subscription] => This\Subscription Object
(
[id] => sub_xxx
[current_period_end] => 1770507654
[current_period_start] => 1767715454
[plan] => This\Plan Object
(
[currency] => usd
[product] => 333
)
[start_date] => 1767715654
[status] => active
)
)
[2] => This\Product Object
(
[id] => 222
[name] => Test Plan 2
)
[3] => This\Product Object
(
[id] => 111
[name] => Test Plan 1
[subscription] => This\Subscription Object
(
[id] => sub_xxx
[current_period_end] => 1670307654
[current_period_start] => 1667715654
[plan] => This\Plan Object
(
[currency] => usd
[product] => 111
)
[start_date] => 1667715654
[status] => active
)
)
)
[has_more] =>
)
I know I can perform two foreach loops on each object and turn them into arrays with the revenant data I need and combine them that way, but I would rather merge them complete as objects.
How can I merge/combine these two in PHP?
Edit: This was my attempt at merging the two, but I am lost on how to actually make it work:
foreach($array1 as $product) {
foreach($array2 as $subscription) {
if ($subscription->plan->product == $product->id){
//how do I append to the object in the right place (where the 0 is)?
$array1->data[0]->subscription = $subscription->plan;
}
}
}

how to fetch data from json? [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
i have data in json something like that:
stdClass Object
(
[contacts] => stdClass Object
(
[14] => stdClass Object
(
[data] => stdClass Object
(
[email] => veer#gmail.com
[first_name] => veer
[last_name] =>
[user_id] => 16
[owner_id] => 0
[optin_status] => 0
[date_created] => 2020-01-20 13:14:54
[ID] => 14
[gravatar] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[age] =>
)
[meta] => stdClass Object
(
[preferences_changed] => 1579526094
[user_login] => veer
[profile_picture] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[birthday] =>
)
[tags] => Array
(
[0] => 92
[1] => 13
[2] => 12
[3] => 9
)
[files] => Array
(
)
)
[9] => stdClass Object
(
[data] => stdClass Object
(
[email] => singh.pratibha1432#gmail.com
[first_name] => Pratibha
[last_name] => Singh
[user_id] => 8
[owner_id] => 0
[optin_status] => 0
[date_created] => 2020-01-20 13:14:54
[ID] => 9
[gravatar] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[age] =>
)
[meta] => stdClass Object
(
[preferences_changed] => 1579526094
[user_login] => pratibha
[profile_picture] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[birthday] =>
)
[tags] => Array
(
[0] => 94
[1] => 93
[2] => 92
[3] => 91
[4] => 82
[5] => 15
[6] => 14
[7] => 13
[8] => 9
)
[files] => Array
(
)
)
[4] => stdClass Object
(
[data] => stdClass Object
(
[email] => singh.dev1432#gmail.com
[first_name] => Devesh
[last_name] => Singh
[user_id] => 7
[owner_id] => 0
[optin_status] => 0
[date_created] => 2020-01-20 13:14:54
[ID] => 4
[gravatar] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[age] =>
)
[meta] => stdClass Object
(
[preferences_changed] => 1579526094
[user_login] => devesh
[profile_picture] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[birthday] =>
)
[tags] => Array
(
[0] => 93
[1] => 92
[2] => 15
[3] => 12
[4] => 11
[5] => 9
)
[files] => Array
(
)
)
[2] => stdClass Object
(
[data] => stdClass Object
(
[email] => admin#gmail.com
[first_name] => veronica
[last_name] =>
[user_id] => 1
[owner_id] => 0
[optin_status] => 0
[date_created] => 2020-01-20 13:14:54
[ID] => 2
[gravatar] => http://localhost:8888/wordpress/wp-content/uploads/avatars/1/5dc525d984494-bpfull.jpg
[age] =>
)
[meta] => stdClass Object
(
[preferences_changed] => 1579526094
[user_login] => admin
[profile_picture] => http://localhost:8888/wordpress/wp-content/uploads/avatars/1/5dc525d984494-bpfull.jpg
[birthday] =>
)
[tags] => Array
(
[0] => 94
[1] => 92
[2] => 15
[3] => 14
[4] => 13
[5] => 9
)
[files] => Array
(
)
)
)
[status] => success
)
and now I am trying to fetch "email" from the contacts->id->data->email so for that i am using this code. I am trying to loop the things by which i can fetch email from all the ids present. and in the ids section, there is data and inside data the email is present so how can i fetch all emails from all the ids.
foreach ((Array)$body->contacts as $id => $values) {
foreach ($values as $data => $value) {
$emails = array('email'=>$value->email);
return $emails;
}
}
but it return only single data:
Array
(
[email] => veer#gmail.com
)
i want to fetch all the email like that:
Array
(
[0] => Array
(
[emails] => veer#gmail.com
)
[1] => Array
(
[emails] => singh.pratibha1432#gmail.com
)
[2] => Array
(
[emails] => singh.dev1432#gmail.com
)
[3] => Array
(
[emails] => admin#gmail.com
)
)
how can I achieve this?
Define null array and in foreach run store every email in that variable.
$emails = array();
foreach ((Array)$body->contacts as $id => $values) {
foreach ($values as $data => $value) {
$emails[] = array('emails'=>$contact->data->email;);
}
}
return $emails;

How to access object properties within an array

I have some code that generates an array with an object in it and I want to access the object values.
$results = array();
$std_arr = array();
$data['examlist'] = $this->exam_model->get($exam);
foreach ($student_array as $key => $value) {
$std_arr[] = $value->student_id;
$results[] = $this->student_model->get_single_student_result($value->student_id);
}
and When i log the results i got
Array
(
[0] => Array
(
[0] => stdClass Object
(
[exam_schedule_id] => 14
[get_marks] => 23.00
[id] => 1
[exam_id] => 1
[teacher_subject_id] => 1
[name] => GEO
[teacher_id] => 16
[subject_id] => 1
[code] => Geography
)
)
[1] => Array
(
[0] => stdClass Object
(
[exam_schedule_id] => 14
[get_marks] => 0.00
[id] => 1
[exam_id] => 1
[teacher_subject_id] => 1
[name] => GEO
[teacher_id] => 16
[subject_id] => 1
[code] => Geography
)
How can I access these 'stdClass Object' property?

How to convert DB table with parent son relation to multi-dimensional array

How can I convert a database table like that:
into a multi-dimensional array like that ?
I tried a recursive loop like below but can't get it to display properly.
function cost_centres_format($items)
{
foreach ($items as $item) {
echo $item->name.' - '.$item->parent_id;
echo '<br/>';
$sons = $this->purchase_order_model->get_cost_centre_sons($item->internal_purchase_order_cost_centre_id);
if(count($sons)>0){
$this->cost_centres_format($sons);
}
}
}
Here is my solution for it:
function cost_centres_format($items,$parent_id,$array=array()) {
foreach($items as $item) {
if($item->parent_id == $parent_id) {
$array[] = $item;
if($item->internal_purchase_order_cost_centre_id>0) {
$array = cost_centres_format($items,$item->internal_purchase_order_cost_centre_id,$array);
}
}
}
return $array;
}
$array = cost_centres_format($items,0);
Diesel (id:5) will be below Vehicle Maintenance (id:4) because of its original order. You can do an additional sort by name but in your example Capital (id:3) was below Overheads (id:2).
The above code doesn't produce multidimensional array but instead one dimensional array like so :
Array
(
[0] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 1
[name] => Direct Expenses
[parent_id] => 0
)
[1] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 4
[name] => Vehicle Maintenance
[parent_id] => 1
)
[2] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 9
[name] => CN09 AKO
[parent_id] => 4
)
[3] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 10
[name] => DY52 BYO
[parent_id] => 4
)
[4] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 14
[name] => MX08 MVJ
[parent_id] => 4
)
[5] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 15
[name] => YJ55 TXA
[parent_id] => 4
)
[6] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 5
[name] => Diesel
[parent_id] => 1
)
[7] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 6
[name] => Vehicle Rent
[parent_id] => 1
)
[8] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 11
[name] => Vehicle Repair
[parent_id] => 1
)
[9] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 16
[name] => CN09 AKO
[parent_id] => 11
)
[10] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 17
[name] => DY52 BYO
[parent_id] => 11
)
[11] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 18
[name] => MX08 MVJ
[parent_id] => 11
)
[12] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 19
[name] => YJ55 TXA
[parent_id] => 11
)
[13] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 2
[name] => Overheads
[parent_id] => 0
)
[14] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 12
[name] => Internet Service
[parent_id] => 2
)
[15] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 13
[name] => Warehouse Rent
[parent_id] => 2
)
[16] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 3
[name] => Capital
[parent_id] => 0
)
[17] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 7
[name] => New Stock
[parent_id] => 3
)
[18] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 8
[name] => New Vehicle
[parent_id] => 3
)
)

Append Object from one Object Array to Another Object Array

I have two Object arrays. "ID" from array 1 corresponds to the same as "media_id" in array 2 I need to add the "album_ids" of array 2 to array 1.
Object 1;
Array ( [0] => stdClass Object ( [ID] => 2482 [post_author] => 6 [post_date] => 2014-07-31 07:59:26) [1] => stdClass Object ( [ID] => 2483 [post_author] => 6 [post_date] => 2014-07-31 07:59:28)
Object 2=
Array ( [0] => stdClass Object ( [album_id] => 52 [media_id] => 2482 ) [1] => stdClass Object ( [album_id] => 92 [media_id] => 2483 )
I need the end result to be:
Array ( [0] => stdClass Object ( [ID] => 2482 [post_author] => 6 [post_date] => 2014-07-31 07:59:26 [album_id] => 52) [1] => stdClass Object ( [ID] => 2483 [post_author] => 6 [post_date] => 2014-07-31 07:59:28 [album_id] => 92)
Thank you for the help!!
As per your question, lets suppose you have two arrays of objects - array1 and array2.
aarry1 = Array( [0] => stdClass Object ( [ID] => 2482 [post_author] => 6 [post_date] => 2014-07-31 07:59:26) [1] => stdClass Object ( [ID] => 2483 [post_author] => 6 [post_date] => 2014-07-31 07:59:28)
array2= Array ( [0] => stdClass Object ( [album_id] => 52 [media_id] => 2482 ) [1] => stdClass Object ( [album_id] => 92 [media_id] => 2483 )
Now try out this code.
$albumids = array();
foreach ( $array2 as $key => $val) {
$albumids[$val->media_id] = $val->album_id;
}
if(!empty($albumids)) {
foreach ( $array1 as $key => $val) {
if(isset($albumids[$val->ID])) {
$val->album_id = $albumids[$val->ID];
}
}
}
print_r($array1);
You would get the expected result
Edit: I had to correct the following line from:
$val->albumid = $albumids[$val->id];
to
$val->album_id = $albumids[$val->ID];

Categories