PHP数组与键合并

时间:2016-08-14 18:51:09

标签: php arrays array-merge

如何合并这两个数组:

Array
(
    [uczrrtawpxfjanycwwlqygoq] => Array
    (
        [user_id] => 53
        [value] => Boris
        [key] => uczrrtawpxfjanycwwlqygoq
    )

    [dreamhack] => Array
    (
        [user_id] => 263
        [value] => More
        [key] => dreamhack
    )

)

我的第二个数组需要添加到第一个

的键中
Array
(
    [dreamhack] => Array
    (
        [viewers] => 32229
        [channel] => Array
            (
                [broadcaster_language] => en
                [display_name] => Dreamhack
                [_id] => 22859340
                [created_at] => 2011-06-09T06:11:52Z
                [updated_at] => 2016-08-14T18:34:36Z
                [delay] => 
                [banner] => 
                [background] => 
                [partner] => 1
                [views] => 36258931
                [followers] => 79892
                [_links] => Array
                    (
                        [self] =>
                        [teams] => 
                    )

            )

    )

)

执行简单的数组合并会提供原始数组,而不是组合数组。因此,对于dreamhack,我需要一个aeeay,所有标签组合[user_id],[value],[key],[viewers],[channel]和subarray。

2 个答案:

答案 0 :(得分:0)

使用array_merge_recursive,专门用于执行此类操作。引用文档:

  

array_merge_recursive()合并一个或多个数组的元素   一起使得一个值附加到结尾   前一个。它返回结果数组。

     

如果输入数组具有相同的字符串键,则为其值   这些键合并到一个数组中,这样就完成了   递归地,如果其中一个值是一个数组本身,那么   函数将它与另一个数组中的相应条目合并   太。但是,如果数组具有相同的数字键,则后者   值不会覆盖原始值,但会被追加。

答案 1 :(得分:0)

在评论中提到..这是你想要的吗?

<pre>
<?php

$array1 = [
    'uczrrtawpxfjanycwwlqygoq' => [
        'user_id' => 53,
        'value' => 'Boris',
        'key' => 'uczrrtawpxfjanycwwlqygoq'
    ],

    'dreamhack' => [
        'user_id' => 263,
        'value' => 'More',
        'key' => 'dreamhack'
    ]

];


$array2 = [
    'dreamhack' => [
        'viewers' => 32229,
        'channel' => [
                'broadcaster_language' => 'en',
                'display_name' => 'Dreamhack',
                '_id' => 22859340,
                'created_at' => '2011-06-09T06:11:52Z',
                'updated_at' => '2016-08-14T18:34:36Z',
                'delay' => '',
                'banner' => '',
                'background' => '',
                'partner' => 1,
                'views' => 36258931,
                'followers' => 79892,
                '_links' => [
                        'self' => '',
                        'teams' => ''
                    ]

            ]

    ]

];

$result = array_merge_recursive ($array1, $array2);
var_dump($result);
?>
</pre>

结果如下:

array(2) {
  ["uczrrtawpxfjanycwwlqygoq"]=>
  array(3) {
    ["user_id"]=>
    int(53)
    ["value"]=>
    string(5) "Boris"
    ["key"]=>
    string(24) "uczrrtawpxfjanycwwlqygoq"
  }
  ["dreamhack"]=>
  array(5) {
    ["user_id"]=>
    int(263)
    ["value"]=>
    string(4) "More"
    ["key"]=>
    string(9) "dreamhack"
    ["viewers"]=>
    int(32229)
    ["channel"]=>
    array(12) {
      ["broadcaster_language"]=>
      string(2) "en"
      ["display_name"]=>
      string(9) "Dreamhack"
      ["_id"]=>
      int(22859340)
      ["created_at"]=>
      string(20) "2011-06-09T06:11:52Z"
      ["updated_at"]=>
      string(20) "2016-08-14T18:34:36Z"
      ["delay"]=>
      string(0) ""
      ["banner"]=>
      string(0) ""
      ["background"]=>
      string(0) ""
      ["partner"]=>
      int(1)
      ["views"]=>
      int(36258931)
      ["followers"]=>
      int(79892)
      ["_links"]=>
      array(2) {
        ["self"]=>
        string(0) ""
        ["teams"]=>
        string(0) ""
      }
    }
  }
}