Home  >  Article  >  php教程  >  php中有关合并某一字段键值相同的数组合并的改进

php中有关合并某一字段键值相同的数组合并的改进

WBOY
WBOYOriginal
2016-06-06 20:06:561385browse

这篇文章主要介绍了php中有关合并某一字段键值相同的数组合并的改进,需要的朋友可以参考下

下面是实现代码:

/** **关于参数的说明 **$key键值相同的键名 **$array代表原数组 **$start代表$array[0][$key] **$newkey代表相同键值相同的键名 **/ 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; }

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn