<?php function combine($array,$start,$key,$newkey){ static $new; //静态变量 foreach($array as $k=>$v){ if($v[$key]==$start){ $new[$v[$newkey]][] = $v; unset($array[$k]); continue; } } sort($array); if(count($array)!==0){ combine($array,$array[0][$key],$key,$newkey);//递归调用 } return $new; } $array=array(); $array=array(array(‘a‘=>‘aaa‘,‘b‘=>‘bbb‘),array(‘a‘=>‘aaa‘,‘b‘=>‘ccc‘),array(‘a‘=>‘123‘,‘b‘=>‘3456‘)); $key=‘a‘; $newkey=‘a‘; $list=combine($array,$array[0][$key],$key,$newkey); foreach($list as $key=>$val){ $new_list[]=$val; } print_r($new_list); /** * Array ( [0] => Array ( [0] => Array ( [a] => aaa [b] => bbb ) [1] => Array ( [a] => aaa [b] => ccc ) ) [1] => Array ( [0] => Array ( [a] => 123 [b] => 3456 ) ) ) */ ?>
时间: 2024-12-08 18:30:37