문제

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.

도움이 되었습니까?

해결책

Make a non-anonymous function:

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

usort($flights, 'sortFlights');
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top