PHP 二维数组根据某个字段进行排序
程序员文章站
2022-07-03 11:34:50
...
<?php
$arr = array(
array(
'id' => 5,
'time' => '2018-01-05'
),
array(
'id' => 3,
'time' => '2018-01-03'
),
array(
'id' => 1,
'time' => '2018-01-01'
),
array(
'id' => 2,
'time' => '2018-01-02'
),
array(
'id' => 4,
'time' => '2018-01-04'
)
);
$arrSort = array();
foreach($arr AS $key => $value){
foreach($value AS $k=>$v){
$arrSort[$k][$key] = $v;
}
}
/**
$arrSort=Array
(
[id] => Array
(
[0] => 5
[1] => 3
[2] => 1
[3] => 2
[4] => 4
)
[time] => Array
(
[0] => 2018-01-05
[1] => 2018-01-03
[2] => 2018-01-01
[3] => 2018-01-02
[4] => 2018-01-04
)
)
*/
array_multisort($arrSort['time'], SORT_ASC, $arr);
print_r($arr);
<?php
$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'];
}
array_multisort($volume, SORT_DESC, $data);
print_r($data);
?>
上一篇: 学习Java代理模式,一篇就足够够了!
下一篇: C段http_banner获取v0.3