sort an array base on key

前端 未结 2 1567
暗喜
暗喜 2021-01-18 23:54

i have an array like this:

Array
(
    [0] => Array
        (
            [title] => some title
            [time] => 1279231500
        )

    [1]          


        
相关标签:
2条回答
  • 2021-01-19 00:33

    You can sort it this way (since it is an associative array):

    function cmp($a, $b)
    {
       return strcmp($a['time'], $b['time']);
    }
    
    usort($your_array, "cmp");
    print_r($your_array);
    
    0 讨论(0)
  • 2021-01-19 00:45

    As Gumbo mentioned, you should not use strcmp for integer values.

    Use this function

    function cmp($a, $b) {
        if ($a['time'] == $b['time'])
            return 0;
        return ($a['time'] < $b['time']) ? -1 : 1;
    }
    
    0 讨论(0)
提交回复
热议问题