- print_r: documentation ( source)
- array_filter: documentation ( source)
- array_pop: documentation ( source)
<?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));