/* * 二维数组 按某列排序 * array_multisort($arr1,$arr2) * 手册 例子如下 * */ $data[] = array(volume = 67, edition = 2);$data[] = array(volume = 86, edition = 1);$data[] = array(volume = 85, edition = 6);$data[] = array(volume = 98, edition
/*
*/
$data[] = array('volume' => 67, 'edition' => 2); $data[] = array('volume' => 86, 'edition' => 1); $data[] = array('volume' => 85, 'edition' => 6); $data[] = array('volume' => 98, 'edition' => 2); $data[] = array('volume' => 86, 'edition' => 6); $data[] = array('volume' => 67, 'edition' => 7);
// 取得列的列表 foreach ($data as $key => $row) { $volume[$key] = $row['volume']; $edition[$key] = $row['edition']; } // 将数据根据 volume 降序排列,根据 edition 升序排列 // 把 $data 作为最后一个参数,以通用键排序 array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
function sigcol_arrsort($data,$col,$type=SORT_DESC){ if(is_array($data)){ $i=0; foreach($data as $k=>$v){ if(key_exists($col,$v)){ $arr[$i] = $v[$col]; $i++; }else{ continue; } } }else{ return false; } array_multisort($arr,$type,$data); return $data; } print_r(sigcol_arrsort($data,'edition',SORT_DESC));
运行结果:
7 array('volume' => 67, 'edition' => 7);
6 array('volume' => 86, 'edition' => 6);
6 array('volume' => 85, 'edition' => 6);
2 array('volume' => 98, 'edition' => 2);
2 array('volume' => 67, 'edition' => 2);
1 array('volume' => 86, 'edition' => 1);