PHP让数组中有相同值的组成新的数组实例
下面小编就为大家分享一篇PHP让数组中有相同值的组成新的数组实例,具有很好的参考价值,希望对大家有所帮助,一起跟随小编过来看看吧。
实例如下所示:
- $arr = array(
- 0=>array('key1'=>'value1' , 'key2'=>'value2'),
- 1=>array('key1'=>'value1' , 'key2'=>'value3'),
- 2=>array('key1'=>'value2' , 'key2'=>'value4'),
- 999=>array('key1'=>'value2' , 'key2'=>'value5')
- );
- $result = array();
- <span >foreach ($arr as $data) {
- isset($result[$data['key1']]) || $result[$data['key1']] = array();
- $result[$data['key1']][] = $data['key2'];
- }</span>
- ksort($cityAr, SORT_NATURAL);//这个是键值按字母先后顺序排列
- print_r($result);
- //输出如下
- Array
- (
- [value1] => Array
- (
- [0] => value2
- [1] => value3
- )
- [value2] => Array
- (
- [0] => value4
- [1] => value5
- )
- )