我有一個數組,看起來像這樣:PHP排序多維數組
Array
(
[13] => Array
(
[name] => Blah blah
[description] => Blah blah blah
[parent_group_id] => 8
[display] => Blah : Blah
[stamps] => Array
(
[73] => Array
(
[stamp_id] => 73
[name] => Blah
[is_member] =>
[rank] => 2 <--
[description] => Blah blah blah
[rank_token] => easy
)
[80] => Array
(
[stamp_id] => 80
[name] => Blah
[is_member] =>
[rank] => 1 <--
[description] => Blah blah blah
[rank_token] => medium
)
[73] => Array
(
[stamp_id] => 73
[name] => Blah
[is_member] =>
[rank] => 4 <--
[description] => Blah blah blah
[rank_token] => easy
)
[80] => Array
(
[stamp_id] => 80
[name] => Blah
[is_member] =>
[rank] => 3 <--
[description] => Blah blah blah
[rank_token] => medium
)
)
)
)
然而,我要通過[「秩」]保持它們是1,2中的數字時,這個陣列中的對象進行排序,3和4.(我在代碼示例中添加了箭頭)因此,在使用usort之後,我希望所有內容都按數字順序排序。所以我希望它看起來像這樣:
Array
(
[13] => Array
(
[name] => Blah blah
[description] => Blah blah blah
[parent_group_id] => 8
[display] => Blah : Blah
[stamps] => Array
(
[80] => Array
(
[stamp_id] => 80
[name] => Blah
[is_member] =>
[rank] => 1 <--
[description] => Blah blah blah
[rank_token] => medium
)
[73] => Array
(
[stamp_id] => 73
[name] => Blah
[is_member] =>
[rank] => 2 <--
[description] => Blah blah blah
[rank_token] => easy
)
[80] => Array
(
[stamp_id] => 80
[name] => Blah
[is_member] =>
[rank] => 3 <--
[description] => Blah blah blah
[rank_token] => medium
)
[73] => Array
(
[stamp_id] => 73
[name] => Blah
[is_member] =>
[rank] => 4 <--
[description] => Blah blah blah
[rank_token] => easy
)
)
)
)
我的實際數組要大得多,但它仍然遵循這種模式。
我認爲這將有助於: –
michi
2013-03-12 21:59:46
你剛纔提到'usort',你試過了什麼? 'foreach($ array as $ key => $ value)usort($ array [$ key] ['stamps'],'yourusortfunction');' – Wrikken 2013-03-12 22:00:13