不知道我錯過了某些顯而易見的事情,但我無法在其他答案中找到排序多維數組子組的方法,而無需觸摸主要順序。僅對多維數組進行PHP排序子組
我有一個數組這樣
Array
(
[1] => Array
(
[name] => Apples
[type] => 1
[sales] => 10
)
[2] => Array
(
[name] => Apples
[type] => 2
[sales] => 30
)
[3] => Array
(
[name] => Apples
[type] => 3
[sales] => 20
)
[4] => Array
(
[name] => Oranges
[type] => 1
[sales] => 30
)
[5] => Array
(
[name] => Oranges
[type] => 2
[sales] => 10
)
[6] => Array
(
[name] => Oranges
[type] => 3
[sales] => 20
)
[7] => Array
(
[name] => Lemons
[type] => 1
[sales] => 10
)
[8] => Array
(
[name] => Lemons
[type] => 2
[sales] => 30
)
[9] => Array
(
[name] => Lemons
[type] => 3
[sales] => 20
)
)
我需要做的就是通過密鑰「銷售」排序每個「名」組(蘋果,橙子和檸檬),剩下的鍵「名稱的順序「蘋果,橙子和檸檬不變,所以結果應該是這樣的:
Array
(
[1] => Array
(
[name] => Apples
[type] => 1
[sales] => 10
)
[2] => Array
(
[name] => Apples
[type] => 3
[sales] => 20
)
[3] => Array
(
[name] => Apples
[type] => 2
[sales] => 30
)
[4] => Array
(
[name] => Oranges
[type] => 2
[sales] => 10
)
[5] => Array
(
[name] => Oranges
[type] => 3
[sales] => 20
)
[6] => Array
(
[name] => Oranges
[type] => 1
[sales] => 30
)
[7] => Array
(
[name] => Lemons
[type] => 1
[sales] => 10
)
[8] => Array
(
[name] => Lemons
[type] => 3
[sales] => 20
)
[9] => Array
(
[name] => Lemons
[type] => 2
[sales] => 30
)
)
我找不到任何方式做到這一點,任何人都可以給我一個關於這個提示?
謝謝!
'usort','usort','首先的usort' –
@gege安排你的數組中的PHP的形式,告訴你試過嗎? –
您發佈的「代碼」中的數組結構不清楚。請發佈一個有效的php arrray定義。 – arkascha