How to interconnect two arrays?

2

If I have an array:

Array
(
    [0] => Array
        (
            [color] => azul
        )
    [1] => Array
        (
            [color] => branca
        )
    [2] => Array
        (
            [color] => prata
        )
    [3] => Array
        (
            [color] => preta
        )
    [4] => Array
        (
            [color] => roxa
        )
    [5] => Array
        (
            [color] => vermelha
        )
)

And another:

Array
(
    [0] => Array
        (
            [COUNT(color)] => 3
        )
    [1] => Array
        (
            [COUNT(color)] => 1
        )
    [2] => Array
        (
            [COUNT(color)] => 1
        )
    [3] => Array
        (
            [COUNT(color)] => 6
        )
    [4] => Array
        (
            [COUNT(color)] => 1
        )
    [5] => Array
        (
            [COUNT(color)] => 6
        )
)

And I want to interact with them by leaving them like this:

Array
(
    [0] => Array
        (
            [color] => azul
            [COUNT(color)] => 3
        )
    [1] => Array
        (
            [color] => branca
        )
    [2] => Array
        (
            [color] => prata
            [COUNT(color)] => 3
        )
    [3] => Array
        (
            [color] => preta
            [COUNT(color)] => 3
        )
    [4] => Array
        (
            [color] => roxa
            [COUNT(color)] => 3
        )
    [5] => Array
        (
            [color] => vermelha
            [COUNT(color)] => 3
        )
)

Or maybe otherwise because I need to use both values in 1 foreach declaring, (color name) and next (quantity).

    
asked by anonymous 03.06.2015 / 21:28

1 answer

1
<?php
$x = Array(0 => Array('a' => 'b'), 1 => Array('c' => 'd'));
$y = Array(0 => Array('e' => 'f'), 1 => Array('g' => 'h'));

var_dump(array_map(array_merge, $x, $y));
?>

Output:

array(2) {
  [0]=>
  array(2) {
    ["a"]=>
    string(1) "b"
    ["e"]=>
    string(1) "f"
  }
  [1]=>
  array(2) {
    ["c"]=>
    string(1) "d"
    ["g"]=>
    string(1) "h"
  }
}

What we want to do with each element of the individual arrays is array_merge ( which combines the keys of both dictionaries); array_map applies array_merge to each element of both arrays individually and returns the response.

    
03.06.2015 / 21:36