Jump to content

Recommended Posts

I apologize ahead of time if the code I post is too large. I did not see any posts regarding code length.

 

I have the following array, that I created to be traversed and have specific parts passed into specific functions.

 

$options = array(
   'navigation' => array(
       'page_title' => __('Aisis', 'aisis'),
       'menu_title' => __('Aisis', 'aisis'),
       'capabillity' => 'edit_themes',
       'menu_slug' => 'aisis-core-options',
       'function' => 'some_function',
       'icon_url' => '',
       'position' => '',
       'sub_menues' => array(
           array(
         'page_title' => __('Aisis', 'aisis'),
         'menu_title' => __('Aisis', 'aisis'),
         'capabillity' => 'edit_themes',
         'menu_slug' => 'aisis-core-options',
         'function' => 'some_function',
  ), 
           array(
         'page_title' => __('Aisis', 'aisis'),
         'menu_title' => __('Aisis', 'aisis'),
         'capabillity' => 'edit_themes',
         'menu_slug' => 'aisis-core-options',
         'function' => 'some_function',
  ), 
       )
   ),
);

 

There are two main functions here that will be used add_menu_page() for the $options['navigation'] and add_submenu_page() for the $options['navigation']['submenues'] (yes I know there's a spelling mistake in submenues).

 

The following code is a mess, how ever does almost what I need. Please bare with me, as It's no where near a finished product:

 

foreach($options as $setting=>$option){
   if($setting == 'navigation' && is_array($option)){
    if(isset($option[$k])){
        echo $option['page_title'];
    }


       foreach($option as $k=>$v){
        if(is_array($v)){
         foreach($v as $sub_menu){
     	 foreach($sub_menu as $sk=>$sv){
     	  if(isset($sub_menu[$sk])){
     	   echo $sub_menu['menu_slug'];
     	  }
     	 }
         }
        }
       }
   }
}

 

 

Now what you'll see is something like echo $sub_menu['menu_slug'] - well, what if that key is not set? simple enough you say:

 

if(isset($sub_menu['menu_slug'])){}else{}

 

accept not, because this is where the functions come in handy, each key represents a value which is an argument for the function. no the function does not take an array of arguments, so, the question is:

 

How do I take the array above and the code provided and do something like:

 

       foreach($option as $k=>$v){
           if(is_array($v)){
               foreach($v as $sub_menu){
                   foreach($sub_menu as $sk=>$sv){
                       if(isset($sub_menu[$sk])){
                           add_submenu_page($sub_menu['menu_slug'] /*...and so on....*/)
                       }
                    }
                }
           }
       }

 

with out having to do a bunch of if is set, do this, else do that.I have to be able to pass in each individual key as a argument to the respected functions listed above, but I don't want to have to do a bunch of isset statements.

 

I was thinking of doing something like in_array, or creating a walker that would look for the functions arguments.

 

In case your wondering what the functions arguments are, look at the keys. they are the arguments, the values are the arguments values.

 

So this is where I ask the community, what do I do? is there a simpler, cleaner way to achieve what I want?

Link to comment
https://forums.phpfreaks.com/topic/274295-multidimensional-array-of-arrays-help/
Share on other sites

After having a quick look at this, I think I'd go for a recursive function. Create a function to display a one-dimensional menu, and then check for an array in the menu item. If it is, call the function again with that array as the parameter.

 

Quite basic, but you might need to alter the source array a bit to be identical for sub menu items and main menu items.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.