简体   繁体   中英

sorting multiple indexes array in php

how to sort this kind of array? this will be sort by array[x][1] . can this be sorted using usort?

Array
(
    [0] => Array
        (
            [0] => 1247
            [1] => 3
            [2] => no
            [3] => no
        )

    [1] => Array
        (
            [0] => 224
            [1] => 1
            [2] => no
            [3] => no
        )

    [2] => Array
        (
            [0] => 226
            [1] => 2
            [2] => no
            [3] => no
        )

)

You're on the right track with usort() , you simply need to compare against the 2 nd element in the array ( as you required ).

usort($array, function($i, $v) {
    return $i[1] - $v[1];
});

Note : The above $array is your array that you want to sort.

Which will return it in the correct order ( 1 , 2 then 3 ).

Example/Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM