PHP Sort a multidimensional array by element containing date

后端 未结 10 1845
野趣味
野趣味 2020-11-22 09:02

I have an array such as:

Array
(
[0] => Array
    (
        [id] => 2
        [type] => comment
        [text] => hey
        [datetime] => 20         


        
10条回答
  •  执笔经年
    2020-11-22 09:55

    http://us2.php.net/manual/en/function.array-multisort.php see third example:

     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'];
    }
    
    array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
    
    ?>
    

    fyi, using a unix (seconds from 1970) or mysql timestamp (YmdHis - 20100526014500) would be be easier for the parser but i think in your case it makes no difference.

提交回复
热议问题