0

I want to order an array with customer comments by the field date. From the most current date to the oldest.

$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"
]
....

I want to get this result:

$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"
  ]

How can I do this within my Laravel controller or with PHP?

EDIT: I'm trying with usort but I get it wrong

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

This return me this error:

usort() expects parameter 2 to be a valid callback, function 'compare' not found or invalid function name
RichardMiracles
  • 2,032
  • 12
  • 28
  • 46
  • I think this link would be the answer to you. [https://stackoverflow.com/questions/37567751/laravel-sort-an-array-by-date/52368760](https://stackoverflow.com/questions/37567751/laravel-sort-an-array-by-date/52368760) – Kevin Lin Mar 21 '20 at 12:04
  • I think this article would be help to you. [https://stackoverflow.com/questions/37567751/laravel-sort-an-array-by-date/52368760](https://stackoverflow.com/questions/37567751/laravel-sort-an-array-by-date/52368760) – Kevin Lin Mar 21 '20 at 12:05

2 Answers2

1

Assuming $testimonials is a collection object, you can make use of sortByDesc() to sort by dates in decreasing order.

<?php 

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

dd($testimonials);
nice_dev
  • 17,053
  • 2
  • 21
  • 35
0

In Laravel you can do it like:

$array = collect($testimonials)->sortBy('date')->reverse()->toArray();

And in PHP you can use usort to achieve the same

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

Sehdev
  • 5,486
  • 3
  • 11
  • 34
  • @AntonioMorales Change `usort($testimonials, 'compare');` to `usort($testimonials, $this->compare);` – Sehdev Mar 21 '20 at 12:02
  • Now throw out this new bug: Undefined property: App\Http\Controllers\WebController::$compare – RichardMiracles Mar 21 '20 at 12:04
  • @AntonioMorales Instead of `function` try `anonymous function` : `usort($testimonials, function($a, $b){ return strtotime($b['date']) - strtotime($a['date']) ; });` – Sehdev Mar 21 '20 at 12:05
  • Not working : usort() expects parameter 2 to be a valid callback, no array or string given – RichardMiracles Mar 21 '20 at 12:07
  • try this: `$array = collect($testimonials)->sortBy('date')->reverse()->toArray();` – Sehdev Mar 21 '20 at 12:12
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/210054/discussion-between-sehdev-and-antonio-morales). – Sehdev Mar 21 '20 at 12:16