Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
137 views
in Technique[技术] by (71.8m points)

php - objects array, order by date

i have an array of objects that I want to sort by the date field, I have tried several examples but I am unable to get it. I share an image of the array and the code I am trying to use. Any help is welcome, thank you!

Array Image

        function build_sorter($key) {
            return function ($a, $b) use ($key) {
                return strnatcmp($a[$key], $b[$key]);
            };
        }
        $NuevoArrayPedidos= usort($ArrayPedidos, $this->build_sorter('fecha'));

        return $NuevoArrayPedidos;

ERROR: Warning: usort() expects parameter 1 to be array, object given in /home/vestatex/gestionWebs.vestatex.es/controllers/pedidosController.php on line 82 (USORT LINE)

question from:https://stackoverflow.com/questions/65882220/objects-array-order-by-date

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think this can help you
I convert the time to the int and then compare it
And you ['fecha'][0] for getting the time

function date_compare($a, $b)
{
    $t1 = strtotime($a['fecha'][0]);
    $t2 = strtotime($b['fecha'][0]);
    return $t1 - $t2;
}
    
$NuevoArrayPedidos = usort($ArrayPedidos, 'date_compare');
return $NuevoArrayPedidos;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...