Question

I was working on my local server where I have php 5.3+ and usort with anonymous works great.

But on the hosting provider they have php 5.2.17 where usort doesn't work in the way I used it.

Here is the code On Local [PHP 5.3+]

class Flight {
    ...
    var $dateLeg;
    ...
}
usort($flights, function($a, $b) {
    return strtotime($a->dateLeg) - strtotime($b->dateLeg);
});

usort here is called in a different function in different file. And $flights contains objects of Flight Class.

How to sort it in PHP 5.2.17.

Please help.

Was it helpful?

Solution

Make a non-anonymous function:

function sortFlights($a, $b) {
    return strtotime($a->dateLeg) - strtotime($b->dateLeg);
}

usort($flights, 'sortFlights');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top