Mind gem Mind gem - 3 months ago 15
PHP Question

PHP array merge with keys

How do I merge these two arrays:

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

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

)


And my second array which needs to be added to the keys of the first

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] =>
)

)

)

)


Doing a simple array merge gives the original array and not a combined one. So for dreamhack I would require one aeeay with all the tags combined [user_id], [value], [key], [viewers], [channel] and subarray.

Answer

as asked in the comment .. is this what you want ?

<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>

result looks like:

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