在数组中声明数组显示php中的错误

时间:2014-03-31 11:11:18

标签: php arrays

显示

  

解析错误:语法错误,意外' ['在第1行

$test=  array (( [0] => array ( ['href'] => 'admin/manageusers', ['title'] => 'Setting', ['icon'] => 'icon-user', ['sub_menu'] => array ( [0] => array ( ['href'] => 'admin/manageusers/hoteladmins/edit-hoteladmin/29', ['title'] => 'Profile' ), [1] => array ( ['href'] => 'admin/manageusers/employee', ['title'] =>'Users' ), [2] => array ( ['href'] => 'admin/manageusers/pointssetting', ['title'] => 'Points' ), [3] => array ( ['href'] => 'admin/manageusers/transaction', ['title'] => 'Transaction' ) ) ) ) );

3 个答案:

答案 0 :(得分:1)

声明不应将[0]用作关键字,只需使用0,所以:

$test = array(
    0 => array(
        ['href']     => 'admin/manageusers',
        ['title']    => 'Setting',
        ['icon']     => 'icon-user',
        ['sub_menu'] => array(
            0 => array(
                ['href']  => 'admin/manageusers/hoteladmins/edit-hoteladmin/29',
                ['title'] => 'Profile'
            ),
            1 => array(
                ['href']  => 'admin/manageusers/employee',
                ['title'] => 'Users'
            ),
            2 => array(
                ['href']  => 'admin/manageusers/pointssetting',
                ['title'] => 'Points'
            ),
            3 => array(
                ['href']  => 'admin/manageusers/transaction',
                ['title'] => 'Transaction'
            )
        )
    )
);

此外,如果您想设置增加键,可以跳过这些键:

$test = array(
    array(
        ['href']     => 'admin/manageusers',
        ['title']    => 'Setting',
        ['icon']     => 'icon-user',
        ['sub_menu'] => array(
            array(
                ['href']  => 'admin/manageusers/hoteladmins/edit-hoteladmin/29',
                ['title'] => 'Profile'
            ),
            array(
                ['href']  => 'admin/manageusers/employee',
                ['title'] => 'Users'
            ),
            array(
                ['href']  => 'admin/manageusers/pointssetting',
                ['title'] => 'Points'
            ),
            array(
                ['href']  => 'admin/manageusers/transaction',
                ['title'] => 'Transaction'
            )
        )
    )
);

答案 1 :(得分:1)

不要使用 SQUARE ([])括号来定义新元素,它会没问题。

看看正确的版本:

$test = array(
    0 => array(
        'href' => 'admin/manageusers',
        'title' => 'Setting',
        'icon' => 'icon-user',
        'sub_menu' => array(
            0 => array(
                'href' => 'admin/manageusers/hoteladmins/edit-hoteladmin/29',
                'title' => 'Profile'
            ),
            1 => array('href' => 'admin/manageusers/employee', 'title' => 'Users'),
            2 => array('href' => 'admin/manageusers/pointssetting', 'title' => 'Points'),
            3 => array('href' => 'admin/manageusers/transaction', 'title' => 'Transaction')
        )
    )
);

答案 2 :(得分:0)

$test = array(
array(
    'href' => 'admin/manageusers',
    'title' => 'Setting',
    'icon' => 'icon-user',
    'sub_menu' => array(
        array(
            'href' => 'admin/manageusers/hoteladmins/edit-hoteladmin/29',
            'title' => 'Profile'
        ),
        array('href' => 'admin/manageusers/employee', 'title' => 'Users'),
        array('href' => 'admin/manageusers/pointssetting', 'title' => 'Points'),
        array('href' => 'admin/manageusers/transaction', 'title' => 'Transaction')
    )
  )
);

删除所有"[""]",如上所述