1 <?php 2 $a=[ 3 4 ‘a‘=>[‘d‘=>[‘aa‘=>1,‘bb‘=>2,‘cc‘=>3]], 5 ‘b‘=>[‘f‘=>[‘dd‘=>4,‘ee‘=>5,‘ff‘=>6]], 6 ‘c‘=>[‘g‘=>[‘gg‘=>7,‘hh‘=>8,‘ii‘=>9]], 7 ]; 8 9 10 $b=reduce_arr($a); 11 echo ‘<pre>‘; 12 var_dump($b); 13 14 15 function reduce_arr ($arr) { 16 17 static $temp = array(); // 声明了一个静态局部变量 18 19 foreach ($arr as $key => $val) { 20 if (is_array($val)) { 21 reduce_arr($val); 22 } else { 23 $temp[$key] = $val; 24 } 25 } 26 return $temp; 27 } 28 ?>
时间: 2024-10-14 14:04:09