Even using the array_merge
, you’ll always lose one of them, the best you can do is get something like this using the array_merge_recursive
:
array_merge_recursive($el1, $el2);
#saida:
array([code]=>array([0]=>A..01 [1]=>A..02) [name]=>array([0]=>Al...M...s [1]=>Al...> M...s))
Because the elements of both arrays have the same index. Unless you put them into a variable as two distinct groups.
$novo= array();
array_push($novo, $el1);
array_push($novo, $el2);
#saida:
array([0]=>array([code]=>...[name]) [1]=>array([code]=>...[name]))
Or else:
$novo= array();
$novo[] = $el1;
$novo[] = $el2;
#saida:
array([0]=>array([code]=>...[name]) [1]=>array([code]=>...[name]))
Another even simpler way is by using the union operator +
instead of the array_merge
:
$novo = $el1 + $el2;
There are still other ways to accomplish this process, but your question lacks details.
Using array_merge($categoria_1, $categoria_2) works... But if one of the initial arrays is NULL, it won’t.. Any hint...
– Rafael Schaffer Gimenes
Try casting to see if it goes: array_merge((array)$categoria_1, (array)$categoria_2)
– JuniorNunes