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中的“ID”对应于数组2中的“media_id”,我需要将数组2的“album_ids”添加到数组1中。
Object 1;
对象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)
数组([0] => stdClass对象([ID] => 2482 [post_author] => 6 [post_date] => 2014-07-31 07:59:26)[1] => stdClass对象([ID] = > 2483 [post_author] => 6 [post_date] => 2014-07-31 07:59:28)
Object 2=
对象2 =
Array ( [0] => stdClass Object ( [album_id] => 52 [media_id] => 2482 ) [1] => stdClass Object ( [album_id] => 92 [media_id] => 2483 )
数组([0] => stdClass对象([album_id] => 52 [media_id] => 2482)[1] => stdClass对象([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)
数组([0] => stdClass对象([ID] => 2482 [post_author] => 6 [post_date] => 2014-07-31 07:59:26 [album_id] => 52)[1] => stdClass对象([ID] => 2483 [post_author] => 6 [post_date] => 2014-07-31 07:59:28 [album_id] => 92)
Thank you for the help!!
感谢您的帮助!!
1 个解决方案
#1
1
As per your question, lets suppose you have two arrays of objects - array1 and array2.
根据您的问题,假设您有两个对象数组 - array1和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)
aarry1 = Array([0] => stdClass对象([ID] => 2482 [post_author] => 6 [post_date] => 2014-07-31 07:59:26)[1] => stdClass对象([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 )
array2 = Array([0] => stdClass对象([album_id] => 52 [media_id] => 2482)[1] => stdClass对象([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];
编辑:我必须更正以下行:$ val-> albumid = $ albumids [$ val-> id]; to $ val-> album_id = $ albumids [$ val-> ID];
#1
1
As per your question, lets suppose you have two arrays of objects - array1 and array2.
根据您的问题,假设您有两个对象数组 - array1和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)
aarry1 = Array([0] => stdClass对象([ID] => 2482 [post_author] => 6 [post_date] => 2014-07-31 07:59:26)[1] => stdClass对象([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 )
array2 = Array([0] => stdClass对象([album_id] => 52 [media_id] => 2482)[1] => stdClass对象([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];
编辑:我必须更正以下行:$ val-> albumid = $ albumids [$ val-> id]; to $ val-> album_id = $ albumids [$ val-> ID];