Question:
I have this:
$categoria_1[0] = array(
'code' => 'ALI000001',
'name' => 'Alimento > Arroz
);
$categoria_2[0] = array(
'code' => 'ALI000002',
'name' => 'Alimento > Massas
);
And I need to leave it like this:
$category[0] = [{'code' => 'ALI000001', 'name' => 'Alimento > Arroz},{'code' => 'ALI000002', 'name' => 'Alimento > Massas}]
Does anyone have any tips?
Answer:
Even using array_merge
, you will always miss one of them, the best you can do is get something similar to this using 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 in 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 yet:
$novo= array();
$novo[] = $el1;
$novo[] = $el2;
#saida:
array([0]=>array([code]=>...[name]) [1]=>array([code]=>...[name]))
Another even simpler way is to use the union operator +
instead of array_merge
:
$novo = $el1 + $el2;
There are still other ways to go about this process, but your question lacks details.