phparraysarray-key

How to get the current key name while building an array


I am building a PHP menu and I want to have an array used to define the menu items. The array would be something like this:

$menuItems = [
    "menu_title" => [
        "view_type" => [
            "name" => "display_name",
            "url"  => "?p=view_type&t=menu_title",
        ],
    ],
];

where "menu_title" and "view_type" would act as keys for the main array and secondary array, but would also act as URL paramenters values. For example:

$menuItems = [
    "user_accounts" => [
        "list" => [
            "name" => "User list",
            "url"  => "?p=list&t=user_accounts",
        ],
        "edit" => [
            "name" => "Edit user",
            "url"  => "?p=edit&t=user_accounts",
        ],
    ],
    "user_profile" => [
        "list" => [
            "name" => "Profile list",
            "url"  => "?p=list&t=user_profile",
        ],
    ],
];

Is there a way in which I could refrence within the array value it's own key name? Something along the lines of

$menuItems = [
    "menu_title" => [
        "view_type" => [
            "name" => "display_name",
            "url"  => "?p=<<name of key holding the key holding this value>>&t=<<name of key holding the key holding the key holding this value>>",
        ],
    ],
];

For further context, after defining the array, I use it to define the menu itself with a function:

$this->addMenu("Users", [
    $menuItems["user_accounts"]["list"]["name"] => $menuItems["user_accounts"]["list"]["url"],
]);

I know I can use key(), or array_search(), or array_keys() and so on, but everything I've found so far requires me to iterate through the array after its being declared - what I want to achieve is refrencing the key name while the array is built, so that I don't have to needlesly loop through the array just to do this.

Or I could just go around and define the piece that needs url values while defining the menu itself, or just type the values myself if I'm typing the keynames anyways:

$this->addMenu("Users", [
    $menuItems["user_accounts"]["list"]["name"] => "?p=list&t=user_accounts",
]);

But I'm looking for a way to avoid doing that because people other than myself will be using this to define new menu items or edit them and whenever there's an user-input involved, there's bound to be typing errors, or they miss changing the url values and so on. My endgoal would be:

My logic/intuition tells me that's not really possible, as if the array has no been built yet, it's not stored/defined anywhere and the key names are not "mapped" - is there a way I am wrong and this is actually achievable?


Solution

  • You cannot reference the keys of the array when you're declaring the array, but you can do this when you are using the array. Like this:

    <?php
    
    $menuItems = [
        "menu_title" => [
            "view_type" => [
                "name" => "display_name",
                "url"  => "hello.html",
            ],
        ],
    ];
    
    foreach ($menuItems as $menuItemKey => $menuItem) {
        foreach ($menuItem as $menuLinkKey => $menuLink) {
            $name = $menuLink['name'];
            $url  = $menuLink['url'] . "?p=$menuLinkKey&t=$menuItemKey";
            echo '<a href="' . $url . '">' . $name . '</a>';
        }    
    }
    

    The output is:

    <a href="hello.html?p=view_type&t=menu_title">display_name</a>
    

    Live demo: https://3v4l.org/WQXsY

    See the foreach control structure