-1

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?

3
  • Most answers come with usort.
    – xdazz
    Commented Nov 2, 2011 at 9:57
  • 1
    In what way are you sorting it? The only difference I can see is that you're using associative keys instead of numerical?
    – Marcus
    Commented Nov 2, 2011 at 9:58
  • possible duplicate of PHP array sort using inner val
    – hakre
    Commented Nov 2, 2011 at 9:59

3 Answers 3

1

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

I Found answer

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

it's work

0

array_column() with a null second parameter leave the value payload untouched while the third parameter will determine which column is used as the new keys.

Functional-style key replacements in a new array: Demo

var_export(
    array_map(
        function ($set) {
            $set['subMenu'] = array_column($set['subMenu'], null, 'subMenuName');
            return $set;
        },
        $menu
    )
);

or modify the original array by reference. Demo

foreach ($menu as ['subMenu' => &$sub]) {
    $sub = array_column($sub, null, 'subMenuName');
}
var_export($menu);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.