[英]How to sort associative array based on key in php
我正在構建具有自定義排序功能的嵌套菜單。 我被困在對每個菜單的子菜單項進行排序的過程中。 如何對用戶定義順序的關於,服務等子菜單項進行排序? 任何幫助將不勝感激。
Array
(
[About] => Array
(
[0] => Array
(
[0] => About us
[1] => about.php
)
[5] => Array
(
[0] => Who we are
[1] => who-we-are.php
)
)
[Services] => Array
(
[5] => Array
(
[0] => Web Design
[1] => web-design.php
)
[10] => Array
(
[0] => Web development
[1] => web-development.php
)
[15] => Array
(
[0] => SEO
[1] => seo.php
)
)
)
我們將對每個子菜單數組(例如關於我們,服務)分別進行排序。 但是,如果我們添加一個外循環,我們可以立即對其進行排序
foreach ($subMenuArray as $key => $row)
{
$innerArray = $row; // This is the array that holds the menu items
$value = $row[0]; // That is the word About us, Who we are
// Here we build an array that holds the actual values we want to sort.
$sortingReference[$key] = $value;
}
// Using array_multisort we are sorting our main array
// referencing to the array we built above
// Sort the subMenuArray with menu items descending
array_multisort($sortingReference, SORT_DESC, $subMenuArray);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.