Maison > Article > développement back-end > PHP多维数组多字段排序分组,处理的数据比较多,望高手解决
原数组
$data = array( 0 => array( 'id' => '510851539496070', 'name' => 'A', 'title' => '108A', 'time' => '1389230133', ), 1 => array( 'id' => '510851539496070', 'name' => 'A', 'title' => '108A', 'time' => '1389230129', ), 2 => array( 'id' => '510940988802058', 'name' => 'A', 'title' => '109A', 'time' => '1389241098', ), 3 => array( 'id' => '510868001733128', 'name' => 'A', 'title' => '109A', 'time' => '1389230169', ), 4 => array( 'id' => '510558107108314', 'name' => 'A', 'title' => '132A', 'time' => '1389191699', ), 5 => array( 'id' => '509763928740655', 'name' => 'A', 'title' => '136A', 'time' => '1389225520', ), 6 => array( 'id' => '509763928740655', 'name' => 'A', 'title' => '143A', 'time' => '1389225520', ), 7 => array( 'id' => '509763928740655', 'name' => 'A', 'title' => '143A', 'time' => '1389225520', ), 8 => array( 'id' => '510851539496070', 'name' => 'A', 'title' => '245A', 'time' => '1389230129', ), 9 => array( 'id' => '510558107108314', 'name' => 'A', 'title' => '256A', 'time' => '1389191699', ), 10 => array( 'id' => '510868001733128', 'name' => 'A', 'title' => '262A', 'time' => '1389230169', ), 11 => array( 'id' => '509763928740655', 'name' => 'A', 'title' => '306A', 'time' => '1389225521', ), 12 => array( 'id' => '509763928740655', 'name' => 'A', 'title' => '306A', 'time' => '1389225520', ), 13 => array( 'id' => '509763928740655', 'name' => 'A', 'title' => '315A', 'time' => '1389225522', ), 14 => array( 'id' => '510868001733128', 'name' => 'A', 'title' => '403A', 'time' => '1389230169', ), 15 => array( 'id' => '510558107108314', 'name' => 'A', 'title' => '404A', 'time' => '1389191699', ), 16 => array( 'id' => '510558107108314', 'name' => 'A', 'title' => '404A', 'time' => '1389191699', ), 17 => array( 'id' => '510558107108314', 'name' => 'A', 'title' => '404A', 'time' => '1389191699', ), 18 => array( 'id' => '510558107108314', 'name' => 'A', 'title' => '404A', 'time' => '1389191699', ), 19 => array( 'id' => '510558107108314', 'name' => 'A', 'title' => '404A', 'time' => '1389191699', ), 20 => array( 'id' => '510558107108314', 'name' => 'B', 'title' => '40B', 'time' => '1389191699', ), );
array( 0 => array( 'id' => '510851539496070', 'name' => 'A', 'title' => '108A', 'time' => '1389230129', ), 1 => array( 'id' => '510851539496070', 'name' => 'A', 'title' => '108A', 'time' => '1389230133', ), 2 => array( 'id' => '510851539496070', 'name' => 'A', 'title' => '245A', 'time' => '1389230129', ), 3 => array( 'id' => '510868001733128', 'name' => 'A', 'title' => '109A', 'time' => '1389230169', ), 4 => array( 'id' => '510868001733128', 'name' => 'A', 'title' => '262A', 'time' => '1389230169', ), 5 => array( 'id' => '510868001733128', 'name' => 'A', 'title' => '403A', 'time' => '1389230169', ), 6 => array( 'id' => '510940988802058', 'name' => 'A', 'title' => '109A', 'time' => '1389241098', ), 7 => array( 'id' => '510558107108314', 'name' => 'A', 'title' => '132A', 'time' => '1389191699', ), 8 => array( 'id' => '510558107108314', 'name' => 'A', 'title' => '256A', 'time' => '1389191699', ), 9 => array( 'id' => '510558107108314', 'name' => 'A', 'title' => '404A', 'time' => '1389191699', ), 10 => array( 'id' => '510558107108314', 'name' => 'A', 'title' => '404A', 'time' => '1389191699', ), 11 => array( 'id' => '510558107108314', 'name' => 'A', 'title' => '404A', 'time' => '1389191699', ), 12 => array( 'id' => '510558107108314', 'name' => 'A', 'title' => '404A', 'time' => '1389191699', ), 13 => array( 'id' => '510558107108314', 'name' => 'A', 'title' => '404A', 'time' => '1389191699', ), 14 => array( 'id' => '510558107108314', 'name' => 'B', 'title' => '40B', 'time' => '1389191699', ), 15 => array( 'id' => '509763928740655', 'name' => 'A', 'title' => '136A', 'time' => '1389225520', ), 16 => array( 'id' => '509763928740655', 'name' => 'A', 'title' => '143A', 'time' => '1389225520', ), 17 => array( 'id' => '509763928740655', 'name' => 'A', 'title' => '143A', 'time' => '1389225520', ), 18 => array( 'id' => '509763928740655', 'name' => 'A', 'title' => '306A', 'time' => '1389225520', ), 19 => array( 'id' => '509763928740655', 'name' => 'A', 'title' => '306A', 'time' => '1389225521', ), 20 => array( 'id' => '509763928740655', 'name' => 'A', 'title' => '315A', 'time' => '1389225522', ), );
//先排序title,time foreach ($data as $key => $row) { $title[$key] = $row['title']; $time[$key] = $row['time']; } array_multisort($title, SORT_ASC, SORT_REGULAR, $time, SORT_ASC, SORT_REGULAR, $data); //相同id临时放到一起 $tem = array(); foreach ($data as $value) { $tem[$value['id']][] = $value; } //输出最终结果 $result = array(); foreach ($tem as $sub_arr) { foreach ($sub_arr as $value) { $result[] = $value; } } print_r($result);
这样不行吗
foreach($data as $r) { $id[] = $r['id']; $title[] = $r['title']; $time[] = $r['time'];}array_multisort($id, $title, $time, $data);print_r($data);
这样不行吗
foreach($data as $r) { $id[] = $r['id']; $title[] = $r['title']; $time[] = $r['time'];}array_multisort($id, $title, $time, $data);print_r($data);
//输出最终结果
$result = call_user_func_array('array_merge', $tem);
我来学习一下
土豪村长兄么暂时想法和你一样
http://www.9958.pw/post/php_array_multi_sort