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];
Related
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;
I have an array
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 79
[name] => shelin
[status] => 0
)
[1] => stdClass Object
(
[id] => 80
[name] => shanu
[status] => 2
)
[2] => stdClass Object
(
[id] => 81
[name] => linto
[status] => 2
)
[3] => stdClass Object
(
[id] => 82
[name] => joseph
[status] => 0
)
)
)
I want to rearrange this array by status desc order
I try
usort($usersdetailsA, function($a, $b) {
return $a->status <=> $b->status;
});
I got an error like
usort() expects parameter 1 to be array, object given
I tried
$usersdetailsA = $this->$usersdetailsA->getValues();
the i got an error like
Undefined property:
TCG\Voyager\Http\Controllers\Users::$[{"id":79,"name":"shelin","status":0},{"id":80,"name":"shanu","status":"2"},{"id":81,"name":"linto","status":"2"},{"id":82,"name":"joseph","status":0}]
Expected output
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 80
[name] => shanu
[status] => 2
)
[1] => stdClass Object
(
[id] => 81
[name] => linto
[status] => 2
)
[2] => stdClass Object
(
[id] => 79
[name] => shelin
[status] => 0
)
[3] => stdClass Object
(
[id] => 82
[name] => joseph
[status] => 0
)
Any help would be appreciated.Thanks in advance
You can sort a collection with sort methods by a given key:
$sorted = $collection->sortByDesc('status');
I have product options that came from database and I want to merge all the values by same product_id .
I'm using a foreachloop.
stdClass Object(
[id] => 22
[product_id] => 48
[values] => Array (
[0] => stdClass Object (
[id] => 81
[option_id] => 22
[name] => Black
[value] => black
)
)
),
stdClass Object(
[id] => 10
[product_id] => 50
[values] => Array (
[0] => stdClass Object (
[id] => 33
[option_id] => 10
[name] => L
[value] => l
),
[1] => stdClass Object (
[id] => 34
[option_id] => 10
[name] => M
[value] => m
)
)
),
stdClass Object(
[id] => 24
[product_id] => 48
[values] => Array (
[0] => stdClass Object (
[id] => 98
[option_id] => 11
[name] => X
[value] => x
),
[1] => stdClass Object (
[id] => 99
[option_id] => 11
[name] => XL
[value] => xl
),
[2] => stdClass Object (
[id] => 100
[option_id] => 11
[name] => XLL
[value] => xll
)
)
)
I want to combine these array by same product_id and get output like :
stdClass Object(
[id] => 22
[product_id] => 48
[values] => Array (
[0] => stdClass Object (
[id] => 81
[option_id] => 22
[name] => Black
[value] => black
),
[1] => stdClass Object (
[id] => 98
[option_id] => 11
[name] => X
[value] => x
)
[2] => stdClass Object (
[id] => 99
[option_id] => 11
[name] => XL
[value] => xl
),
[3] => stdClass Object (
[id] => 100
[option_id] => 11
[name] => XLL
[value] => xll
)
),
stdClass Object(
[id] => 10
[product_id] => 50
[values] => Array (
[0] => stdClass Object (
[id] => 33
[option_id] => 10
[name] => L
[value] => l
),
[1] => stdClass Object (
[id] => 34
[option_id] => 10
[name] => M
[value] => m
)
)
)
I'm using foreach loop , How to do this inside foreach loop ?
I want to merge values of same product_id. What is the best way to do this ?
My code is:
foreach($data['products'] as $pro){
foreach($pro->options as $opt){
debug($opt);
}
}
You can try something like this :
1/ Your data
$array = /* array with all your product obj */;
// Create your new result array
$result = array();
2/ The foreach loop
// Loop throught all your object
foreach ($array as $obj) {
// Your current product id
$current_product_id = $obj->product_id;
// The values of this product
$current_product_values = $obj->values;
// Loop through all the value of the product
foreach ($current_product_values as $value) {
// Add those value to your result array for the current product id
$result[$current_product_id][] = $value;
}
}
Is it what you are looking for?
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.
I'm trying to do some kind of function that will find (in the following array) the object with the id of 2, and move it to the top of the array. Here's the original array:
Array
(
[0] => stdClass Object
(
[id] => 177
[startdate] => 2014-08-02
)
[1] => stdClass Object
(
[id] => 178
[startdate] => 2014-08-02
)
[2] => stdClass Object
(
[id] => 2
[startdate] => 2014-07-28
)
[3] => stdClass Object
(
[id] => 82
[startdate] => 2014-07-28
)
[4] => stdClass Object
(
[id] => 199
[startdate] => 2013-10-10
)
)
And here is what I'd like it to output (with the moved array item):
Array
(
[0] => stdClass Object
(
[id] => 2
[startdate] => 2014-07-28
)
[1] => stdClass Object
(
[id] => 177
[startdate] => 2014-08-02
)
[2] => stdClass Object
(
[id] => 178
[startdate] => 2014-08-02
)
[3] => stdClass Object
(
[id] => 82
[startdate] => 2014-07-28
)
[4] => stdClass Object
(
[id] => 199
[startdate] => 2013-10-10
)
)
Any help would be appreciated.
function customShift($array, $id){
foreach($array as $key => $val){ // loop all elements
if($val->id == $id){ // check for id $id
unset($array[$key]); // unset the $array with id $id
array_unshift($array, $val); // unshift the array with $val to push in the beginning of array
return $array; // return new $array
}
}
}
print_r(customShift($data, 2));