PHP 数组与键合并

PHP array merge with keys

如何合并这两个数组:

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.

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

array_merge_recursive() merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

If the input arrays have the same string keys, then the values for these keys are merged together into an array, and this is done recursively, so that if one of the values is an array itself, the function will merge it with a corresponding entry in another array too. If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended.

如评论中所问..这是你想要的吗?

<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) ""
      }
    }
  }
}