3v4l.org

run code in 300+ PHP versions simultaneously
<?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));

preferences:
23.97 ms | 406 KiB | 5 Q