时间:2021-07-01 10:21:17 帮助过:14人阅读
实例如下所示:
$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 style="color:#FF0000;">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 ) )
相关推荐:
实例详解PHP数组式访问接口ArrayAccess
深入理解PHP数组的count函数
php数组合并且去重的示例
以上就是PHP让数组中相同值的组组成新的数组详解的详细内容,更多请关注Gxl网其它相关文章!