1
我的下一個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
)
,我需要的「中華映管」價值首先對其進行排序,然後按「稅」值,最後按「年」計算。
問題是,「cpt」和「tax」的排序順序應該是自定義順序,而不是按字母順序。
而好消息是:
- 總是會產生由「中華映管」完全排序的數組(你可以看到 )。所以我不在乎分類。
- 對於特定的「cpt」,即「Review」,我只需要應用這些排序更改(首先在「稅收」中,然後 「下一年」)。
這是我的代碼,但它是無效的,因爲它也改變了「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;
}
});
任何想法? 非常感謝提前!
檢查在array_multisort正如我在這個問題寫道:_The問題是,「中華映管」和「稅」的排列順序應該是按訂單生產,沒有alphabetically_ – Capiedge