有条件地将项添加到多维数组

时间:2013-05-10 15:39:59

标签: php codeigniter multidimensional-array

我一直在寻找如何做到这一点,我有点难过。 我的数组如下:

$returndata->setup_array = array(
            'General'       =>  array(
                    'Main Details'          => 'setup/maindets',
                    'Directories'           => 'directories',
                    'Extension Allocation'  => 'xtnallo',
                    'List Holidays'         => 'setup/holidays',
                    'List Group VM'         => 'groupvm',
                    'Conference Rooms'      => 'confroom'
                    ),
            'Offices'       =>  array(
                    'List Offices'          => 'iptoffices'
                    ),
            'Users'         =>  array(
                    'List Users'            => 'iptusers'
                    ),
            'Phones'        =>  array(
                    'List Phones'           => 'iptphones'
                    ),
            );

但是我有一个项目在特定条件下(由用户会话触发)需要添加到常规数组的列表中。该部分为'View Details => setup/viewdetails'。我尝试过数组推送(可能是错误的),但这会将该项添加为主数组末尾的另一个数组。

我希望/需要它像这样工作:

$returndata->setup_array = array(
        'General'       =>  array(
                 $viewdets
                'Main Details'          => 'setup/maindets',
                'Directories'           => 'directories',
                'Extension Allocation'  => 'xtnallo',
                'List Holidays'         => 'setup/holidays',
                'List Group VM'         => 'groupvm',
                'Conference Rooms'      => 'confroom'
                ),
        'Offices'       =>  array(
                'List Offices'          => 'iptoffices'
                ),
        'Users'         =>  array(
                'List Users'            => 'iptusers'
                ),
        'Phones'        =>  array(
                'List Phones'           => 'iptphones'
                ),
        );

$viewdets = "'View Details'         => 'setup/viewdetails'";

仍然被解释为用作菜单的功能数组。

3 个答案:

答案 0 :(得分:2)

$returndata->setup_array['General']['View Details'] = 'setup/viewdetails'

干杯里克!

答案 1 :(得分:0)

您可以使用ArrayObject将数组作为参考:

$a  = new ArrayObject();

$b = array(
    "a" => $a
);

$a[] = "foo";
print_r($b);

答案 2 :(得分:0)

您尝试拨打array_push()的是什么?你试过吗

array_push($returndata->setup_array['General'], $viewdets);

您需要将变量添加到您希望它存在的数组的特定深度。在这里查看array_push,还有一个简短的语言语法,可以避免函数调用:

$returndata->setup_array['General'][] = $viewdets;