laravel sort an array by date

后端 未结 4 590
难免孤独
难免孤独 2021-01-06 02:17

hello friends I have an array that looks like this:

array:3 [▼
  0 => array:6 [▼
    \"date\" => \"2016-05-31 15:08:33\"
    0 => \"31 May 16\"
             


        
相关标签:
4条回答
  • 2021-01-06 02:51

    You can use this solution to get the result:

    collect($yourArray)->sortBy('Key')->values();
    
    0 讨论(0)
  • 2021-01-06 02:52

    You can convert to a collection, call the sortBy() and the convert back to an array all on one line.

    $sortedArr = collect($array)->sortBy('date')->all();
    
    0 讨论(0)
  • 2021-01-06 03:00

    you have to create your own function

    array_sort_by_column($array, 'date');
    
    
    function array_sort_by_column(&$array, $column, $direction = SORT_ASC) {
        $reference_array = array();
    
        foreach($array as $key => $row) {
            $reference_array[$key] = $row[$column];
        }
    
        array_multisort($reference_array, $direction, $array);
    }
    
    0 讨论(0)
  • 2021-01-06 03:05

    You can use usort() with custom comarison function.

    function sortByDate($arr1, $arr2)
    {
        $tmp1 = strtotime($arr1['date']);
        $tmp2 = strtotime($arr2['date']);
        return $tmp1 - $tmp2;
    }
    usort($array, 'date');
    
    0 讨论(0)
提交回复
热议问题