Sorting multi dimension array based on values

I have an array that looks like this and I want to sort it by the value ‘RECLABEL’ ascending and by value ‘TOSITE’ low to high.

Array
(
[0] => Array
(
[MAPID] => 1
[RECTYPE] => EARZ
[RECLABEL] => EARZ
[TOSITE] => 0
[ID] => 1
[COUNTER] => Array
(
[0] =>
)

    )

[1] => Array
    (
        [MAPID] => 2
        [RECTYPE] => USALTFUELS
        [RECLABEL] => ALTFUELS
        [TOSITE] => 0.24
        [ID] => 80117
        [COUNTER] => Array
            (
                [0] => 1
            )

    )

[2] => Array
(
[MAPID] => 2
[RECTYPE] => USALTFUELS
[RECLABEL] => ALTFUELS
[TOSITE] => 0.18
[ID] => 58715
[COUNTER] => Array
(
[0] => 1
)

    )

)
The output:

$Array = Array
(
[2] => Array
(
[MAPID] => 2
[RECTYPE] => USALTFUELS
[RECLABEL] => ALTFUELS
[TOSITE] => 0.18
[ID] => 58715
[COUNTER] => Array
(
[0] => 1
)
)

[1] => Array
(
[MAPID] => 2
[RECTYPE] => USALTFUELS
[RECLABEL] => ALTFUELS
[TOSITE] => 0.24
[ID] => 80117
[COUNTER] => Array
(
[0] => 1
)

    )

[0] => Array
(
[MAPID] => 1
[RECTYPE] => EARZ
[RECLABEL] => EARZ
[TOSITE] => 0
[ID] => 1
[COUNTER] => Array
(
[0] =>
)

    )

);

my code is like this and it does seem to work. Anyone can help me? Thanks

function cmp(array $a, array $b) {
 if (($cmp = strcmp($a['RECLABEL'], $b['RECLABEL'])) !== 0) {
     return $cmp;
 } else {
     return $a['TOSITE'] - $b['TOSITE'];
 }
}

usort($array, cmp (array $a, array $b)

so what’s the input, output and expected output? also use the editors </> button to post code or even better use an onlinetool like 3v4l.org so somebody can reproduce that.

I just changed so you can see better. Any help with be great. Thanks.

would be better to post your data as var_export, because it’s easier for your potential helpers to see any problem if you provide a working example, best for copy-and-paste.

Thanks for the tip. I am new to Php and still learning.

Sponsor our Newsletter | Privacy Policy | Terms of Service