I have 2 multidimensional arrays. I need the union of these arrays.
Maybe there is a PHP array function to do this. I tried different ways with array_replace()
, array_merge()
but I couldn't figure it out. The length array are not fixed. The 2 arrays are:
$a = array(
array(
'division'=> 'Dhaka',
'A'=> 0,
'B'=> 0,
'C'=> 0,
'D'=> 0
),
array(
'division'=> 'Chittagong',
'A'=> 0,
'B'=> 0,
'C'=> 0,
'D'=> 0
),
array(
'division'=> 'Rajshahi',
'A'=> 0,
'B'=> 0,
'C'=> 0,
'D'=> 0
),
array(
'division'=> 'Mymensingh',
'A'=> 0,
'B'=> 0,
'C'=> 0,
'D'=> 0
)
);
$b = array(
array("division"=> "Dhaka", "A"=> 5, "B"=> 8),
array("division"=> "Rajshahi", "A"=> 10, "C"=> 2)
);
The result should be:
$a = array(
array(
'division'=> 'Dhaka',
'A'=> 5, //after union
'B'=> 8, //after union
'C'=> 0,
'D'=> 0
),
array(
'division'=> 'Chittagong',
'A'=> 0,
'B'=> 0,
'C'=> 0,
'D'=> 0
),
array(
'division'=> 'Rajshahi',
'A'=> 10, //after union
'B'=> 0, //after union
'C'=> 2,
'D'=> 0
),
array(
'division'=> 'Mymensingh',
'A'=> 0,
'B'=> 0,
'C'=> 0,
'D'=> 0
)
);