【问题标题】:PHP Merger Array ObjectPHP 合并数组对象
【发布时间】:2016-09-10 12:05:18
【问题描述】:

我有一些问题如何合并这个数组。可以帮我吗?

第一个数组:

大批 ( [22] => WP_Post 对象 ( [ID] => 22 [post_author] => 1 ) [23] => WP_Post 对象 ( [ID] => 23 [post_author] => 1 ) )

第二个数组:

大批 ( [0] => 标准类对象 ( [img_thumb] => small_duck.jpg [img_full] => 鸭子.jpg ) [1] => 标准类对象 ( [img_thumb] => small_fish.jpg [img_full] => 鱼.jpg ) )

应该输出:

大批 ( [22] => WP_Post 对象 ( [ID] => 22 [post_author] => 1 [img_thumb] => small_duck.jpg [img_full] => 鸭子.jpg ) [23] => WP_Post 对象 ( [ID] => 23 [post_author] => 1 [img_thumb] => small_fish.jpg [img_full] => 鱼.jpg ) )

数组键跟随第一个数组,

【问题讨论】:

标签: php arrays wordpress


【解决方案1】:

这行得通...

$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);

【讨论】:

  • 已解决,感谢@Barry
猜你喜欢
  • 2015-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多