3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ 1 => [ [ ['catName' => 'Villes', 'catUrl' => 'villes', 'parent' => 151], [ ['catName' => 'Administratif', 'catUrl' => 'territoire', 'parent' => 37], [ ['catName' => 'Gegraphie', 'catUrl' => 'geographie', 'parent' => 0] ] ] ] ] ]; function flattenRows($array) { $result = []; $scalars = array_filter($array, 'is_scalar'); if ($scalars) { $result[] = $scalars; } foreach (array_diff_key($array, $scalars) as $item) { $result = array_merge($result, flattenRows($item)); } return $result; } $result = []; foreach ($array as &$parent) { $parent = flattenRows($parent); } var_export($array);
Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
array ( 1 => array ( 0 => array ( 'catName' => 'Villes', 'catUrl' => 'villes', 'parent' => 151, ), 1 => array ( 'catName' => 'Administratif', 'catUrl' => 'territoire', 'parent' => 37, ), 2 => array ( 'catName' => 'Gegraphie', 'catUrl' => 'geographie', 'parent' => 0, ), ), )

preferences:
110.27 ms | 403 KiB | 120 Q