简体   繁体   中英

Multidimensional array Sort by Key

Having an array like this.

Array
(
    [0] => Array
        (
            [tipo] => Mn
            [tsk] => Vr
            [date] => 14-06
            [J] => 4
            [H] => 0
            [O] => 0
        )

[1] => Array
    (
        [tipo] => Ds
        [tsk] => Mv
        [date] => 14-06
        [J] => 0
        [H] => 0,5
        [O] => 0
    )

[2] => Array
    (
        [tipo] => Vr
        [tsk] => Do
        [date] => 14-06
        [J] => 0
        [H] => 0
        [O] => 5
    )

[3] => Array
    (
        [tipo] => Cl
        [tsk] => REG
        [date] => 14-06
        [J] => 0
        [H] => 4.25
        [O] => 0
    )

[4] => Array
    (
        [tipo] => Cl
        [tsk] => MB10
        [date] => 14-06
        [J] => 0
        [H] => 3.5
        [O] => 0
    )


)

I need to sort it by the J,H & O keys. By this I mean, I need to have all the subarrays that have values on J first, then the ones with values on H and then the ones with values on O. Note: these columns are exclusive from each other, so if one has a value larger than 0, the other two can only have 0.

I've tried many things, but obviously this seemingly simple task is over my head. The closest I got was with array_multisort, but I cannot make it work. I thought something like this should work:

foreach ($data as $key => $row) {
    $J[$key] = $row['J'];
    $H[$key] = $row['H'];
    $O[$key] = $row['O'];
}
array_multisort($J, SORT_ASC, $H, SORT_ASC, $O, SORT_ASC, $data);

But it's not.

Use usort

 usort($data, function($a, $b){
         if (($a["J"] - $b["J"]) != 0) return $a["J"] - $b["J"];
         if (($a["H"] - $b["J"]) != 0) return $a["H"] - $b["H"];
         if (($a["O"] - $b["O"]) != 0) return $a["O"] - $b["O"];
         return 0;             
 });

UPDATE

The above method works only if the values sorted on are numeric, I noticed the the data is not so this should work for you.

 usort($data, function($a, $b){
         if ($a["J"] > $b["J"]) return 1;
         if ($a["J"] < $b["J"]) return -1;
         if ($a["H"] > $b["H"]) return 1;
         if ($a["H"] < $b["H"]) return -1;
         if ($a["O"] > $b["O"]) return 1;
         if ($a["O"] < $b["O"]) return -1;
         return 0;             
 });

 ?>

You must pass an array to array_multisort that is not what you are doing:

Try something like this:

foreach ($data as $key => $row) {
    $sortMe['J'][] = $row['J'];
    $sortMe['H'][] = $row['H'];
    $sortMe['O'][] = $row['O'];
}


array_multisort($sortMe['J'], SORT_DESC, SORT_NUMERIC,
                $sortMe['H'], SORT_ASC, SORT_REGULAR,
                $sortMe['O'], SORT_DESC, SORT_NUMERIC);

Note that sortMe is an array in the letters 'J', 'H', 'O' because I use [] to add the items in the foreach loop, that's what you were doing wrong.

Hope this helps you.

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