阵列的多维关联数组 [英] Multi Dimensional Associative Array of Arrays

查看:162
本文介绍了阵列的多维关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有以下的code:

So I have the following code:

$options = array(
    'navigation' => array(
        'page_title' => 'test',
        'menu_title' => 'test_title',
        'capabillity' => '',
        'menu_slug' => '',
        'function' => '',
        'icon_url' => '',
        'position' => '',
        'sub_menues' => array(
            array(
                'page_title' => 'test',
                'menu_title' => 'test_title',
                'menu_slug' => 'bla'
            ), 
            array(
                'page_title' => 'apples',
                'menu_title' => 'test_apples',
            ), // Set of Navigations
        )
    ),
    'settings' => array(
        array(
            'option_group' => 'bla',
            'option_name' => '',
            'sanitize_call_back' => ''
        )
    ),
    'core_template' => 'path/to/admin/template.phtml'
);

foreach($options as $setting=>$option){
    if($setting == 'navigation' && is_array($option)){
        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'];
                        }
                    }
                }
            }

            if(isset($option[$k])){
                echo $option['page_title'];
            }
        }
    }

    if($setting == 'settings' && is_array($option)){
        foreach($option as $settings_options){
            foreach($settings_options as $sk => $sv){
                if(isset($settings_options[$sk])){
                    echo $settings_options['option_group'];
                }
            }
        }
    }

    if($setting = 'core_template'){
        echo $options['core_template'];
    }
}

其中,是的是一个烂摊子,需要进行重构,工作,直到你在不存在的变量扔。一个好办法,看到这一点:

Which, yes is a mess and needs to be refactored, works UNTIL you throw in a variable that doesn't exist. a good way to see this:

echo $sub_menu['menu_slug'];

现在menu_slug存在于导航/ sub_menues / [0]的数组,但不是在[1]的数组

now menu_slug exists in navigation/sub_menues/[0] array, but not in [1]array.

解决这个问题的典型方法是:

The typical way to fix this is:

if(isset($sub_menu['menu_slug'])){ // do something }

你可以从导航阵列看到,有打算如何过为的LOT 如果设置做到这一点,否则这样做。的,我期待更清洁和更简洁的方式在那里我可以做$ sub_menu ['东西'],它会自动检查是否存在的东西,如果是这样,返回它的值,如果没有做什么,只是忽略它。

How ever as you can see from the navigation array, there is going to be A LOT of if is set do this, else do that. and I am looking for a cleaner and neater way where I can just do $sub_menu['something'] and it automatically checks to see if something exists and if so, returns its value, if NOT do nothing, just ignore it.

现在我有个好主意,我在那里做*如果设置$ sub_menu [$ SK] *,然后我可以这样做:

Now I have the right idea, where I do *if is set $sub_menu[$sk]* and then I could do:

echo $sub_menu[$sk]; 

问题是我打电话在这里发生在参数的函数,参数是关键的值,因此,为什么我要做$ sub_menu ['menu_slug']为例。

the problem is I am calling a function in here that takes in arguments, the arguments are the values of the key, hence why I have to do $sub_menu['menu_slug'] for example.

所以我的问题是:

我怎么做这样的事情:

some_function_call($sub_menu['menu_slug']);

具有做出来的:

if(isset($sub_menu['menu_slug']) && isset($sub_menu['page_title']) /*...and so on...*/){
    some_function_call($sub_menu['menu_slug'], $sub_menu['page_title'] /*..and so on...*/);
}

渔获 - 一些选项,比如(比方说)菜单塞,可能是可选的。

The catch? - some options, such as (say for example) menu slug, might be optional.

任何想法?

推荐答案

好吧,如果你是键唯一性的相对到各个层面,你可以这样做:

Well, if you're keys are unique relatively to all levels, you could do:

array_walk_recursive($options, function($value, $key){
  if($value && in_array($key, array('menu_slug', 'page_title', 'option_group', 'core_template')))
    print $value;  
});

:)

不过,我认为你的真正目的是包装在HTML字符串这个文本,所以你应该使用开关,而不是我的如果语句和打印。如果您需要打印的东西,在一定的顺序,使用临时数组来存储你生成输出,并打印出来就大功告成了。

But I assume that your real purpose is to wrap this text in HTML strings, so you should use switch instead of my if statement and print whatever you want. If you need to print the stuff in a certain order, use a temporary array to store the output you generate, and print it after you're done.

这篇关于阵列的多维关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆