$array1 = [
'ru' => [
'lang' => [
'goods' => [
'товар',
'товара',
'товаров'
],
'main' => [
'title' => 'Title',
'description' => 'Description'
]
]
]
];
$array2 = [
'en' => [
'lang' => [
'goods' => [
'товар',
'товара',
'товаров'
],
'main' => [
'title' => 'My Title',
'description' => 'My Description'
]
]
]
];
$result = [
'lang' => [
'goods' => [
'ru' => [
'товар',
'товара',
'товаров'
],
'en' => [
'товар',
'товара',
'товаров'
]
],
'main' => [
'title' => [
'ru' => 'Title',
'en' => 'My Title'
],
'description' => [
'ru' => 'Description',
'en' => 'My Description'
]
]
]
];
$result = ['lang' => []];
foreach ([$array1, $array2] AS $ar) {
foreach ($ar AS $lang => $data) {
foreach ($data['lang'] AS $k => $v) {
switch ($k) {
case 'goods':
$result['lang'][$k][$lang] = $v;
break;
default:
foreach ($v AS $key => $description) {
$result['lang'][$k][$key][$lang] = $description;
}
}
}
}
}
function walk($data, $key, &$result) {
foreach ($data as $k => $v) {
if (!array_key_exists($k, $result)) {
$result[$k] = [];
}
if (!is_array($v) || array_keys($v) === range(0, count($v) - 1)) {
$result[$k][$key] = $v;
} else {
walk($v, $key, $result[$k]);
}
}
}
function merge(...$arrays) {
$result = [];
foreach ($arrays as $arr) {
$key = array_keys($arr)[0];
walk($arr[$key], $key, $result);
}
return $result;
}
$result = merge($array1, $array2);
$result = [
'lang' => [
'goods' => [
'ru' => $array1['ru']['lang']['goods'],
'en' => $array2['en']['lang']['goods'],
],
'main' => [
'title' => [
'ru' => $array1['ru']['lang']['main']['title'],
'en' => $array2['en']['lang']['main']['title'],
],
'description' => [
'ru' => $array1['ru']['lang']['main']['description'],
'en' => $array2['en']['lang']['main']['description']
]
]
];
$result = [
'lang' => [
'goods' => [
'ru' => $array1['ru']['lang']['goods'],
'en' => $array2['en']['lang']['goods']
],
'main' => [
'title' => [
'ru' => $array1['ru']['lang']['main']['title'],
'en' => $array2['en']['lang']['main']['title']
],
'description' => [
'ru' => $array1['ru']['lang']['main']['description'],
'en' => $array2['en']['lang']['main']['description']
]
]
]
];