Home > Article > Backend Development > Improvements on merging arrays with the same key value of a certain field in PHP_PHP Tutorial
The following is the implementation code:
?
3
4
5
146 13 |
/** **Explanation about parameters **$key Key name with the same key value **$array represents the original array **$start represents $array[0][$key] **$newkey represents the same key name with the same key value **/ function combine_same_val($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_same_val($array,$array[0][$key],$key,$newkey); } return $new; } |