3v4l.org

run code in 300+ PHP versions simultaneously
<?php $testArray = [ 'test1' => 'SingleValue1', 'test2' => 'SingleValue2', 'test3' => [0, 1, 2], 'test4' => [[3, 4, [5, 6, 7]], [8, 9, [10, 11, 12]], 13, 14], 'test5' => [15, 16, 17, [18, 19, 20], 21, [[22]], 23, 24] ]; function deepestArrays(array $array, int $level = 0, array &$lowest = []): array { $subarrays = array_filter($array, 'is_array'); if ($subarrays) { // a deeper level exists foreach ($subarrays as $subarray) { deepestArrays($subarray, $level + 1, $lowest); // recurse each subarray } } else { // deepest level in branch $lowestLevel = key($lowest) ?? $level; // if lowest array is empty, key will be null, fallback to $level value if ($lowestLevel === $level) { $lowest[$level][] = $array; // push the array into the results } elseif ($lowestLevel < $level) { $lowest = [$level => [$array]]; // overwrite with new lowest array } } return current($lowest); // return the deepest array } var_export( deepestArrays($testArray) );
Output for 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
array ( 0 => array ( 0 => 5, 1 => 6, 2 => 7, ), 1 => array ( 0 => 10, 1 => 11, 2 => 12, ), 2 => array ( 0 => 22, ), )

preferences:
76.51 ms | 407 KiB | 5 Q