Brandon
Brandon

Reputation: 189

PHP: Multidimensional array into function call

I am trying to build a simple URL router that loads predefined urls and settings into an array. Then I need to push it all to a function with a static variable so I can store all the urls and settings in a uniform way.

The array looks like:

Array
(
    [index] => Array
        (
            [#title] => Home
            [#access] => user_access
            [#callback] => page_index
        )

    [admin/dashboard] => Array
        (
            [#title] => Dashboard
            [#access_callback] => user_access
            [#page_callback] => page_dashboard
        )

    [admin/stats]

Then I want to push the data from the array into a function:

route('path/path', #callback, #title, #access);

I am trying to build the foreach loops but I can't get passed this mess:

foreach($routes as $path => $array) {
    foreach($array as $key => $value) {

    }
 route($path, );
}

I feel like I am approaching this the wrong way. Any help would be helpful. Thanks

Upvotes: 0

Views: 218

Answers (1)

Willem Ellis
Willem Ellis

Reputation: 5016

What I think you want to do is just break it down to that first array. So:

foreach ( $routes as $path => $array ) {

    route ( $path, $array );

}

Then within route, you would work with the array by just referring to the title, access and callback keys. Like $array['title'] to do something with the title. Just my take on it.

Upvotes: 1

Related Questions