Php 在Laravel中按日期订购数组

Php 在Laravel中按日期订购数组,php,laravel,datetime,laravel-5,Php,Laravel,Datetime,Laravel 5,我想按字段日期订购一个包含客户评论的数组。从最新日期到最早日期 $testimonials = array:356 [▼ 0 => array:4 [▼ "comment" => "blahblahblahblahblah" "name" => "John" "date" => "12/04/2019" ] 1 => array:4 [▼ "comment" => "blah blah blah blah blah" "name

我想按字段日期订购一个包含客户评论的数组。从最新日期到最早日期

$testimonials = array:356 [▼
0 => array:4 [▼
    "comment" => "blahblahblahblahblah"
    "name" => "John"
    "date" => "12/04/2019"
]
1 => array:4 [▼
  "comment" => "blah blah blah blah blah"
  "name" => "Franky V"
  "date" => "13/05/2019"
]
2 => array:4 [▼
  "comment" => "lololololol"
  "name" => "Peter"
  "date" => "06/03/2020"
]
3 => array:4 [▼
  "comment" => "blahblahblahblahblahblahblahblahblah"
  "name" => "Hugo"
  "date" => "24/01/2019"
]
....
我想得到这个结果:

$testimonials = array:356 [▼
  0 => array:4 [▼
    "comment" => "lololololol"
    "name" => "Peter"
    "date" => "06/03/2020"
  ]
  1 => array:4 [▼
    "comment" => "blah blah blah blah blah"
    "name" => "Franky V"
    "date" => "13/05/2019"
  ]
  2 => array:4 [▼
    "comment" => "blahblahblahblahblah"
    "name" => "John"
    "date" => "12/04/2019"
  ]
  3 => array:4 [▼
    "comment" => "blahblahblahblahblahblahblahblahblah"
    "name" => "Hugo"
    "date" => "24/01/2019"
  ]
如何在我的Laravel控制器内或使用PHP实现这一点

编辑:我正在尝试usort,但我弄错了

public function getTrollingDates($testimonials) {
        $currentdate = new DateTime();
        $currentdate = $currentdate->format('Y-m-d');

        foreach($testimonials as &$testimonial) {
            $testimonial['date'] = $this->getRandomDate('2019-01-01',$currentdate);
        }

        $testimonials = usort($testimonials, 'compare');



        return $testimonials;
    }


    public function compare($a, $b)    {
        return  strtotime($b['date']) - strtotime($a['date']) ;
    }
此操作将返回以下错误:

usort() expects parameter 2 to be a valid callback, function 'compare' not found or invalid function name

假设
$estimonials
是一个集合对象,您可以使用按日期降序排序

<?php 

$testimonials = $testimonials->sortByDesc(function($a){
    return DateTime::createFromFormat('d/m/Y',$a['date']);
});

dd($testimonials);

Laravel
中,您可以像这样做:

$array = collect($testimonials)->sortBy('date')->reverse()->toArray();
PHP
中,您可以使用
usort
实现相同的功能

function compare($a, $b)    {

   // if you want to sort by ascending
    return  strtotime($b['date']) - strtotime($a['date']) ;

    //if you want to sort by descending order
    // return  strtotime($a['date'])  - strtotime($b['date'])  ;
}
usort($my_array, 'compare');

@AntonioMorales Change
usort($commissional,'compare')
usort($commentials,$this->compare)现在抛出这个新错误:未定义的属性:App\Http\Controllers\WebController::$compare@AntonioMorales代替
function
try
anonymous function
usort($commentials,function($a,$b){返回strotime($b['date'])-strotime($a['date']);})不工作:usort()希望参数2是有效的回调,没有数组或字符串提供给entry此:
$array=collect($commentials)->sortBy('date')->reverse()->toArray()@AntonioMorales什么样的新日期?它在我的机器上运行良好。我想这个链接就是你的答案。我想这篇文章会对你有所帮助。