foreach ($params as $key => $param) {
if (!is_array($param)) {
$data[$key] = $param;
} else {
foreach ($param as $key2 => $param2) {
if (!is_array($param2)) {
$data[$key . '[' . $key2 . ']'] = $param2;
} else {
foreach ($param2 as $key3 => $param3) {
if (!is_array($param3)) {
$data[$key . '[' . $key2 . '].' . $key3] = $param3;
} else {
foreach ($param3 as $key4 => $param4) {
if (!is_array($param4)) {
$data[$key . '[' . $key2 . '].' . $key3 . '[' . $key4 . '].'] = $param4;
} else {
foreach ($param4 as $key5 => $param5) {
if (!is_array($param5)) {
$data[$key . '[' . $key2 . '].' . $key3 . '[' . $key4 . '].' . $key5] = $param5;
}
}
}
}
}
}
}
}
}
}
function arrayCompressor($data, $key = null, &$result = []): array
{
if (is_array($data)) {
foreach ($data as $index => $param) {
if ($key === null) {
$newKey = $index;
} else {
$newKey = $key . '[' . $index . ']';
}
arrayCompressor($param, $newKey, $result);
}
} else {
$result[$key] = $data;
}
return $result;
}
$array = [
'RoadServiceAutos' => [
[
'RoadServices' => [
[
'CountryAlpha2Code' => 'RU',
'CountryBeta2Code' => 'EN',
],
],
'Test' => "test",
],
],
];
print_r(arrayCompressor($array));