PHP合并数组对象 [英] PHP Merger Array Object
本文介绍了PHP合并数组对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些问题,如何合并数组。能帮助我吗?
i have some problem how merger this array. could help me ?
第一个数组:
Array
(
[22] => WP_Post Object
(
[ID] => 22
[post_author] => 1
)
[23] => WP_Post Object
(
[ID] => 23
[post_author] => 1
)
)
第二个数组:
Array
(
[0] => stdClass Object
(
[img_thumb] => small_duck.jpg
[img_full] => duck.jpg
)
[1] => stdClass Object
(
[img_thumb] => small_fish.jpg
[img_full] => fish.jpg
)
)
应该输出:
Array
(
[22] => WP_Post Object
(
[ID] => 22
[post_author] => 1
[img_thumb] => small_duck.jpg
[img_full] => duck.jpg
)
[23] => WP_Post Object
(
[ID] => 23
[post_author] => 1
[img_thumb] => small_fish.jpg
[img_full] => fish.jpg
)
)
该阵列关键遵循第一阵列,
The array key follow first array,
推荐答案
本作品...
$first = array(
22 => array(
'ID' => 22,
'post_author' => 1
),
23 => array(
'ID' => 23,
'post_author' => 1
)
);
$second = array(
array(
'img_thumb' => 'small_duck.jpg',
'img_full' => 'duck.jpg'
),
array(
'img_thumb' => 'small_fish.jpg',
'img_full' => 'fish.jpg'
)
);
echo '<pre>';
var_dump($first);
var_dump($second);
$i=0;
$should = array();
foreach ($first as $key => $arr) {
if(isset($second[$i]))
$arr = array_merge($arr,$second[$i]);
$should[$key] = $arr;
$i++;
}
var_dump($should);
这篇关于PHP合并数组对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文