简体   繁体   English

有条件地按不同值对多维数组排序

[英]Sort multidimensional array by different values conditionally

I have the next multidimensioanl array (It's quite longer, but I copy here the first elements): 我有下一个multidimensioanl数组(虽然更长,但是我在这里复制了第一个元素):

[0] => Array
    (
        [cpt] => Product
        [tax] => Type A
        [year] => 2012
    )

[1] => Array
    (
        [cpt] => Product
        [tax] => Type B
        [year] => 2012
    )

[2] => Array
    (
        [cpt] => Product
        [tax] => Type C
        [year] => 2011
    )

[3] => Array
    (
        [cpt] => Product
        [tax] => Type C
        [year] => 2011
    )

[4] => Array
    (
        [cpt] => Product
        [tax] => Type D
        [year] => 2015
    )

[5] => Array
    (
        [cpt] => Review
        [tax] => Figure
        [year] => 2014
    )

[6] => Array
    (
        [cpt] => Review
        [tax] => Sample
        [year] => 2012
    )

[7] => Array
    (
        [cpt] => Review
        [tax] => Figure
        [year] => 2012
    )

[8] => Array
    (
        [cpt] => Review
        [tax] => Picture based
        [year] => 2015
    )

[9] => Array
    (
        [cpt] => Review
        [tax] => Figure
        [year] => 2012
    )

[10] => Array
    (
        [cpt] => Review
        [tax] => Picture based
        [year] => 2013
    )
[11] => Array
    (
        [cpt] => Template
        [tax] => L
        [year] => 2013
    )

[12] => Array
    (
        [cpt] => Template
        [tax] => L
        [year] => 2015
    )

[13] => Array
    (
        [cpt] => Template
        [tax] => M
        [year] => 2011
    )

[14] => Array
    (
        [cpt] => Template
        [tax] => S
        [year] => 2011
    )

And I need to sort it first by the "cpt" value, then by the "tax" value, and finally by the "year". 我需要先按“ cpt”值对它进行排序,然后再按“ tax”值对它进行排序,最后再按“ year”对它进行排序。

The problem is that the sorting order for "cpt" and "tax" should be a custom order, not alphabetically. 问题是“ cpt”和“ tax”的排序顺序应该是自定义顺序,而不是字母顺序。

And the good news are: 好消息是:

  • The array always is generated perfectly sorted by "cpt" (as you can see). 数组始终由“ cpt”完美地生成(如您所见)。 So I don't care about sorting it. 所以我不在乎对它进行排序。
  • I only need to apply these sorting changes (in "tax" first, and "year" next) for a specific "cpt", that is "Review". 我只需要为特定的“ cpt”(即“审阅”)应用这些排序更改(首先在“税”中,然后在“年份”中)。

This is my code, but it's not valid, because it changes the "cpt" order also. 这是我的代码,但是无效,因为它也会更改“ cpt”的顺序。

$sort_tax = array("Sample","Figure","Picture based");
usort($the_array, function ($a, $b) use ($sort_tax) {
    $pos_a = array_search($a['tax'], $sort_tax);
    $pos_b = array_search($b['tax'], $sort_tax);
    $result = $pos_a - $pos_b;
    if ($result == 0) {
        $difference = $a['year'] - $b['year'];
        if ($difference >= 0) {
            $value = 0;
        } else {
            $value = 1;
        }
        return $value;
    } else {
        return $pos_a - $pos_b;
    }
});

Any ideas? 有任何想法吗? Many thanks in advance! 提前谢谢了!

$array = array();
foreach($array_name as $arr){
  $array['cpt'] = $arr['cpt'];
  $array['tax'] = $arr['tax'];
  $array['year'] = $arr['year'];
} 

array_multisort($array['tax'], SORT_DESC, $array['year'], SORT_ASC,$Your_array_name);

You can check array_multisort from here 您可以从这里检查array_multisort

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

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