phparrayssorting

How do i sort an array of array in PHP?


Hi I have below 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] => 
                    ) ) )

i need array like below (using the above array),

[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] => 
                    ) ) )

How can i do this?


Solution

  • Looks like you don't need sorting but reindexing. And specific one at that.

    Try this code:

    $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 :)