简体   繁体   English

多维数组按键排序

[英]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. 我需要按J,H和O键对其进行排序。 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. 我的意思是,我需要首先拥有所有在J上具有值的子数组,然后是在H上具有值的子数组,然后是在O上具有值的子数组。 注意:这些列是互斥的,所以如果一个列有一个值大于0,其他两个只能为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. 我得到的最接近的是array_multisort,但是我无法使其工作。 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

 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: 您必须将不是您正在执行的操作的数组传递给array_multisort:

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. 请注意,sortMe是字母“ J”,“ H”,“ O”中的数组,因为我使用[]在foreach循环中添加了项,这就是您做错的。

Hope this helps you. 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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