开发者

Using Recursion to Flatten a PHP Array with Subitems Based on Values (Wasted an Hour on This!)

I'm trying to build a navigation menu. I am receiving an array with a structure like this:

[ 
  [
    Title = A
    Sub items = [
      Title = B
      Sub items = [
        Title = C
      ]
    ]
  ],
  [
    Title = A
    Sub items = [
      Title = B
      Sub items = [
        Title = D
      ]
    ]
  ],
]

I need to take it and make it look like this:

[
  Title = A
  Sub items = [
    Title = B
    Sub items = [
      Title = C,
      Title = D
    ]
  ]
]

I have listed my input and my desired output below. I'm tearing my hair out on this one. I was trying to use recursion, but had some issues with running into infinite loops all over the place. Any help on a good way to tackle this would be so appreciated.

Input:

Array
(
    [0] => Array
        (
            [title] => Dashboard
            [path] => dashboard/
        )

    [1] => Array
        (
            [title] => Settings
            [path] => settings/
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Instances
                            [path] => settings/instances/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Add an Instance
                                            [path] => settings/instances/add-an-instance/
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [title] => Modules
                            [path] => settings/modules/
                        )

                    [2] => Array
                        (
                            [title] => Administrator
                            [path] => settings/administrator/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Change Password
                                            [path] => settings/administrator/change-password/
                                        )

                                )

                        )

                )

        )

    [2] => Array
        (
            [title] => Settings
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Routes
                            [path] => modules/routes/settings/routes/
                        )

                )

        )

    [3] => Array
        (
            [title] => Settings
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Cookies
                            [path] => modules/cookies/settings/cookies/
                        )

                )

        )

    [4] => Array
        (
            [title] => Settings
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Sessions
                            [path] => modules/sessions/settings/sessions/
                        )

                )

        )

    [5] => Array
        (
            [title] => Settings
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Databases
                            [path] => modules/databases/settings/databases/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Add a Database
                                            [path] => modules/databases/settings/databases/add-a-database/
                                        )

                                )

                        )

                )

        )

    [6] => Array
        (
            [title] => Settings
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Models
                            [path] => modules/models/settings/models/
                        )

                )

        )

    [7] => Array
        (
            [title] => Settings
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Users and Accounts
                            [path] => modules/users-and-accounts/settings/users-and-accounts/
                        )

                )

        )

    [8] => Array
        (
            [title] => Users and Accounts
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Users
                            [path] => modules/users-and-accounts/users/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Add a User
                                            [path] => modules/users-and-accounts/users/add-a-user/
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [title] => Accounts
                            [path] => modules/users-and-accounts/accounts/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Add an Account
                                            [path] => modules/users-and-accounts/accounts/add-an-account/
                                        )

                                    [1] => Array
                                        (
                                            [title] => Account Types
                                            [path] => modules/users-and-accounts/accounts/account-types/
                                            [subItems] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [title] => Add an Account Type
                                                            [path] => modules/users-and-accounts/accounts/account-types/add-an-account-type/
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

Output:

Array
(
    [0] => Array
        (
            [title] => Dashboard
            [path] => dashboard/
        )

    [1] => Array
        (
            [title] => Settings
            [path] => settings/
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Instances
                            [path] => settings/instances/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Add an Instance
                                            [path] => settings/instances/add-an-instance/
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [title] => Modules
                            [path] => settings/modules/
                        )

                    [2] => Array
                        (
                            [title] => Administrator
                            [path] => settings/administrator/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Change Password
                                            [path] => settings/administrator/change-password/
                                        )

                                )

                        )

                    [3] => Array
                    (
                        [title] => Routes
                        [path] => modules/routes/settings/routes/
                    )

                    [4] => Array
                        (
                            [title] => Cookies
                            [path] => modules/cookies/settings/cookies/
                        )

                    [5] => Array
                    (
                        [title] => Sessions
                        [path] => modules/sessions/settings/sessions/
                    )

                    [6] => Array
                    (
                        [title] => Databases
                        [path] => modules/databases/settings/databases/
                        [subItems] => Array
                            (
                                [0] => Array
                                    (
                                        [title] => Add a Database
                                        [path] => modules/databases/settings/databases/add-a-database/
                                    )

                            )

                        )

                    [7] => Array
                    (
                        [title] => Models
                        [path] => modules/models/settings/models/
                    )

                    [8] => Array
                    (
                        [title] => Users and Accounts
                        [path] => modules/users-and-accounts/settings/users-and-accounts/
                    )
                )
        )
    [2] => Array
        (
            [title] => Users and Accounts
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Users
                            [path] => modules/users-and-accounts/users/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Add a User
                                            [path] => modules/users-and-accounts/users/add-a-user/
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [title] => Accounts
                            [path] => modules/users-and-accounts/accounts/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Add an Account
                                            [path] => modules/users-and-accounts/accounts/add-an-account/
                                        )

                                    [1] => Array
                                        (
                                            [title] => Account Types
          开发者_如何学Go                                  [path] => modules/users-and-accounts/accounts/account-types/
                                            [subItems] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [title] => Add an Account Type
                                                            [path] => modules/users-and-accounts/accounts/account-types/add-an-account-type/
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )
)


<?php

function flattenArray($subitems) {
    $titles = Array() ;
    foreach($subitems as $subkey => $subvalue) {
        if(!is_array($subvalue) 
            || !isset($subvalue['title'])
            || !isset($subvalue['subItems'])
            || !is_array($subvalue['subItems'])) {

            //Leave this element as-is as it doesn't conform to expectations.
            continue ; 
        }
        if(isset($titles[$subvalue['title']])) {
            foreach($subvalue['subItems'] as $subItem) {
                $subitems[$titles[$subvalue['title']]]['subItems'][] = $subItem ;
            }
            unset($subitems[$subkey]) ;
        } else {
            $titles[$subvalue['title']] = $subkey ;
        }
    }
    foreach($subitems as $subkey => $subvalue) {
        if(is_array($subvalue) && isset($subvalue['subItems'])) {
            $subitems[$subkey]['subItems'] = flattenArray($subvalue['subItems']) ;
        }
    }
    return $subitems ;
}
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜