欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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 二维数组根据某个字段进行排序

<?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);
?>

PHP 二维数组根据某个字段进行排序

相关标签: 二维数组排序