<?php
$inputArray = array(
array(
array(
'value31',
'value32'
),
array(
'value33',
'value34'
),
'value21'
),
'value11'
);
function convertArray($inputArray) {
if(!checkIfAllArraysExceptLast($inputArray)) return $inputArray;
$value = array_pop($inputArray);
$result = [];
foreach($inputArray as $k => $v){
$result[$k] = is_array($v) ? convertArray($v) : $v;
}
return [ $value => $result];
}
function checkIfAllArraysExceptLast($array){
$v = array_pop($array);
return !is_array($v) && count(array_filter($array, fn($v) => is_array($v))) == count($array);
}
print_r(convertArray($inputArray));
- Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.30, 8.2.0 - 8.2.26, 8.3.0 - 8.3.14, 8.4.1
- Array
(
[value11] => Array
(
[0] => Array
(
[value21] => Array
(
[0] => Array
(
[0] => value31
[1] => value32
)
[1] => Array
(
[0] => value33
[1] => value34
)
)
)
)
)
preferences:
69.22 ms | 408 KiB | 5 Q