Did you you mean something like this?
$data = [ 'Hot' => [ 'A' => 5,
'B' => 20,
'C' => 15,
'D' => 10,
'E' => 8
],
'Dead' => [ 'A' => 15,
'B' => 30,
'F' => 55,
'C' => 40,
'G' => 60,
]
];
echo '<pre> BEFORE ' . print_r($data, 1) . '</pre>'; // original array
//
// GET ALL KEYS
//
$keys = [];
foreach ($data as $k1 => $v1) {
$keys = array_merge($keys, array_keys($v1));
}
$keys = array_unique($keys);
$blank_values = array_fill_keys($keys, null);
//
// INSERT MISSING KEYS INTO THE SUBARRAYS
//
foreach ($data as $k => &$subarray) {
$subarray = array_merge($blank_values, $subarray);
}
echo '<pre> AFTER ' . print_r($data, 1) . '</pre>'; // array after processing
which gives...
BEFORE Array
(
[Hot] => Array
(
[A] => 5
[B] => 20
[C] => 15
[D] => 10
[E] => 8
)
[Dead] => Array
(
[A] => 15
[B] => 30
[F] => 55
[C] => 40
[G] => 60
)
)
AFTER Array
(
[Hot] => Array
(
[A] => 5
[B] => 20
[C] => 15
[D] => 10
[E] => 8
[F] =>
[G] =>
)
[Dead] => Array
(
[A] => 15
[B] => 30
[C] => 40
[D] =>
[E] =>
[F] => 55
[G] => 60
)
)