PHP - Merge array with PHP multidirectional array

Asked

Viewed 129 times

0

How to join arrays with a multidimensional array:

Array (
        [0] => 1 
        [1] => 2 
        [2] => 3 
        [3] => 4 
        [4] => 5 
        [5] => Array ( [cod_setor] => 1 ) 
        [6] => Array ( [cod_setor] => 2 ) 
        [7] => Array ( [cod_setor] => 3 ) 
        [8] => Array ( [cod_setor] => 5 ) 
)

See how this my code:

//$item_inventarios = valor recebido da controller codeigniter  

$inicial = $listar_menor_maior_setor[0]['menor_cod_setor'];
$final = $listar_menor_maior_setor[0]['maior_cod_setor'];

$lista = range($inicial, $final);
$total = array_merge($lista,$item_inventarios);

print_r($total);

The expected result would be this:

Array (
        [0] => Array ( [ambiente_setor] => 1 )
        [1] => Array ( [ambiente_setor] => 2 )
        [2] => Array ( [ambiente_setor] => 3 )
        [3] => Array ( [ambiente_setor] => 4 )
        [4] => Array ( [ambiente_setor] => 5 )
        [5] => Array ( [cod_setor] => 1 ) 
        [6] => Array ( [cod_setor] => 2 ) 
        [7] => Array ( [cod_setor] => 3 ) 
        [8] => Array ( [cod_setor] => 5 ) 
)
  • What kind of union would that be? What would be the expected result?

  • The expected result would be this: Array (
 [0] => Array ( [ambiente_setor] => 1 )
 [1] => Array ( [ambiente_setor] => 2 )
 [2] => Array ( [ambiente_setor] => 3 )
 [3] => Array ( [ambiente_setor] => 4 )
 [4] => Array ( [ambiente_setor] => 5 )
 [5] => Array ( [cod_setor] => 1 ) 
 [6] => Array ( [cod_setor] => 2 ) 
 [7] => Array ( [cod_setor] => 3 ) 
 [8] => Array ( [cod_setor] => 5 ) 
)

1 answer

0


There are several possible operations with arrays, several.

For example you can use the operator + to do this, example:

$array = [1,2,4];
$array_b = [5,6,7];
$array_sum = $array + $array_b;
print_r($array_sum); // [1,2,4,5,6,7]

Another way is through the function array_map:

$array_sum = array_map(function () {
   return array_sum(func_get_args());
 }, $array_a, $array_b);

 print_r($array_sum);

You can also use veridic functions:

$array_sum = array_map(function (...$arrays) {
    return array_sum($arrays);
}, $a, $b);

print_r($array_sum);

Adding two arrays is not a framework, but logic, but basically that is, now just take the context and apply in its application and logic.

For more and doubts about how arrays works always look at the documentation before, because the documentation mainly in English is full of references that can be useful for Windows and advise and problems regarding your doubt (mainly the part of arrays).

  • 1

    Thank you for your opinion. The doubt is not adding up two arrays. .. I need to join the arrays being that one of them is multidimetional arrays. None of the above examples would bring the desired result.

  • The correct term would be to concatenate them, so you can use the array_merge or array_merge_recursive, has also the array_replace_recursive, the 3 solve their problem in different ways. I believe that the array_merge_recursive will be enough for your case, but again, I guide you to take a look at the documentation to check how the functions will solve your problem within the context of your application.

  • 1

    I may be wrong, but so much array_merge how much array_merge_recursive just concatenates the arrays. In my case I have an array created dynamically with the function range that creates the array with values from 1 to 5 and the keys are generated in sequence as well. wanted these keys to be generated with a string with a unique value for all keys in the array.

  • Well, I get it, maybe you need to manipulate some things in your array before you reindexe them, here are some exercises about arrays and I think the logic behind this might help you -> https://www.geeksforgeeks.org/minimum-sumtwo-elements-two-arrays-indexes-not/

  • 1

    Perfect. I ended up changing the logic of my system to treat this data in another way.

  • Hello, but Voce can mark what is the right answer please? So it will help other people with the same problem.

Show 1 more comment

Browser other questions tagged

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