【问题标题】:Order a array by date in Laravel在 Laravel 中按日期排序数组
【发布时间】:2020-07-02 08:48:19
【问题描述】:

我想通过字段 date 订购一个包含客户 cmets 的数组。从最新日期到最早日期。

$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

【问题讨论】:

标签: php laravel datetime laravel-5


【解决方案1】:

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

PHP -> usort

【讨论】:

  • @AntonioMorales 将 usort($testimonials, 'compare'); 更改为 usort($testimonials, $this->compare);
  • 现在抛出这个新错误:未定义属性:App\Http\Controllers\WebController::$compare
  • @AntonioMorales 而不是 function 尝试 anonymous function : usort($testimonials, function($a, $b){ return strtotime($b['date']) - strtotime($a['date']) ; });
  • 不工作:usort() 期望参数 2 是一个有效的回调,没有给出数组或字符串
  • 试试这个:$array = collect($testimonials)->sortBy('date')->reverse()->toArray();
【解决方案2】:

假设$testimonials是一个集合对象,你可以利用sortByDesc()按日期降序排序。

<?php 

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

dd($testimonials);

【讨论】:

  • @AntonioMorales 什么样的新约会?它在我的机器上运行良好。
猜你喜欢
  • 2016-09-30
  • 2018-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-19
  • 1970-01-01
  • 2017-10-29
相关资源
最近更新 更多