我如何在PHP中对数组进行排序?

时间:2011-11-02 09:55:11

标签: php arrays sorting

你好我在strustrue下面

[1] => Array
    (
        [menuName] => Apps
        [menuUrl] => /Apps
        [Invisible_Mode] => 
        [subMenu] => Array
            (
                [0] => Array
                    (
                        [subMenuName] => Collaboration
                        [subMenuUrl] => /Apps/Collaboration
                        [subitemCount] => 5
                        [Invisible_Mode] => 
                    )

                [1] => Array
                    (
                        [subMenuName] => Sales
                        [subMenuUrl] => /Apps/Sales
                        [subitemCount] => 11
                        [Invisible_Mode] => 
                    )

                [2] => Array
                    (
                        [subMenuName] => Project Management
                        [subMenuUrl] => /Apps/Project-Management
                        [subitemCount] => 5
                        [Invisible_Mode] => 
                    )

                [3] => Array
                    (
                        [subMenuName] => Human Resources
                        [subMenuUrl] => /Apps/Human-Resources
                        [subitemCount] => 7
                        [Invisible_Mode] => 
                    )

                [4] => Array
                    (
                        [subMenuName] => Marketing
                        [subMenuUrl] => /Apps/Marketing
                        [subitemCount] => 8
                        [Invisible_Mode] => 
                    ) ) )

我需要如下所示的数组(使用上面的数组),

[1] => Array
    (
        [menuName] => Apps
        [menuUrl] => /Apps
        [Invisible_Mode] => 
        [subMenu] => Array
            (
                [Collaboration] => Array
                    (
                        [subMenuName] => Collaboration
                        [subMenuUrl] => /Apps/Collaboration
                        [subitemCount] => 5
                        [Invisible_Mode] => 
                    )

                [Sales] => Array
                    (
                        [subMenuName] => Sales
                        [subMenuUrl] => /Apps/Sales
                        [subitemCount] => 11
                        [Invisible_Mode] => 
                    )

                [Project Management] => Array
                    (
                        [subMenuName] => Project Management
                        [subMenuUrl] => /Apps/Project-Management
                        [subitemCount] => 5
                        [Invisible_Mode] => 
                    )

                [Human Resources] => Array
                    (
                        [subMenuName] => Human Resources
                        [subMenuUrl] => /Apps/Human-Resources
                        [subitemCount] => 7
                        [Invisible_Mode] => 
                    )

                [Marketing] => Array
                    (
                        [subMenuName] => Marketing
                        [subMenuUrl] => /Apps/Marketing
                        [subitemCount] => 8
                        [Invisible_Mode] => 
                    ) ) )

我该怎么做?

2 个答案:

答案 0 :(得分:1)

看起来你不需要排序但重新索引。特别是那个。

试试这段代码:

$original_array = array(
    // your array 
);

$new_array = array();
foreach ($original_array as $menu_item)
{
    if (!empty($menu_item['subMenu']))
    {
        $reindexed_sub_menu =  array();
        foreach ($menu_item['subMenu'] as $sub_menu_item)
        {
            $reindexed_sub_menu[$sub_menu_item['subMenuName']] = $sub_menu_item;
        }

        // if you need to sort submenu by new key enable next line
        // ksort($reindexed_sub_menu);

        $menu_item['subMenu'] = $reindexed_sub_menu;
    }

    $new_array[] = $menu_item;
}

// new array is in $new_array :)

答案 1 :(得分:0)

我找到了答案

foreach($menuCategories as $menuc1) {
                                    foreach($menuc1['subMenu'] as $mc1) {
                                        $key = $mc1['subMenuName'];
                                        $final[$key] = $mc1;
                                        ksort($final);
                                    }
                                    }

它的工作