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
(
=> Array
(
=> value2
=> value3
)
=> Array
(
=> value4
=> value5
)
)
以上这篇PHP让数组中有相同值的组成新的数组实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持CodeAE代码之家。
原文链接:http://blog.csdn.net/jiangnanqbey/article/details/78863688
文档来源:http://www.zzvips.com/article/178351.html
页:
[1]