Merge array via a comparison

Asked

Viewed 60 times

0

I need to compare the arrays using the key called code, and if the keys are equal, join the arrays in a single array, see the example below:

 $array1 = array(array('id' => '1', 'codigo'=>'1155', 'preco' => '25.00'), 
                 array('id' => '2', 'codigo'=>'2280', 'preco' => '47.00')
                 );
 $array2 = array(array('id_produto' => '11', 'codigo'=>'2280', 'nome' => 'Produto Amostra P4', 'id_categoria' => '15'),
                 array('id_produto' => '12', 'codigo'=>'1155', 'nome' => 'Produto Amostra P7', 'id_categoria' => '19')
                );

The expected result would be like the code below:

$arrayResultado = array(array('id_produto' => '11', 'codigo'=>'2280', 'nome' => 'Produto Amostra P4', 'id_categoria' => '15', 'id' => '2', 'preco' => '47.00'), 
                        array('id_produto' => '12', 'codigo'=>'1155', 'nome' => 'Produto Amostra P7', 'id_categoria' => '19','id' => '1', 'preco' => '25.00')
                        );

Someone could help me?

  • See:http://stackoverflow.com/questions/14842272/php-array-mergetwo-arrays-on-same-key

  • did not understand the chunk array_map(Function($a, $b), these variables would be my arrays? @Marconi

  • Marconi didn’t work out so I’m gonna try to do something similar to the idea that you gave me as a reference

  • array_map(function($a, $b) with these two are the call back parameters, here in the documentation -> https://www.php.net/manual/en/function.array-map.php. Voce you can find several examples and how the array_map works throughout PHP versions.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.