I think this is what you’re looking for:
$result = array();
foreach ($arrays as $array) {
$ref = &$result;
foreach ($array as $key => $val) {
if (!isset($ref[$key])) {
$ref[$key] = $val;
} elseif (!is_array($ref[$key])) {
$ref[$key] = array();
}
$ref = &$ref[$key];
}
}
Here the keys are interpreted as path segments to walk the array using a reference. If there is no value yet, the value is stored; if there already is a value, it is replaced by an array.
But this is generating a little different result:
array (
'A' =>
array (
'B' =>
array (
'C' => 3,
),
'D' => 4,
'E' => 5,
'F' =>
array (
'G' => 8,
'H' => 9,
),
),
'X' =>
array (
'Y' => 22,
'Z' => 33,
),
)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…