简体   繁体   English

PHP从多维数组中删除数组

[英]Remove array from multi-dimensional Array in PHP

I build menu on the backend, populated by an array and sub-arrays.我在后端构建菜单,由数组和子数组填充。 The structure is on config.php ..结构在 config.php ..

$primary_nav = array(
   array(
        'name'  => 'Μέλη',
        'url'   => 'member_list.php',
        'icon'  => 'fa fa-users',
        'access'=> 0
    ),array(
        'name'  => 'Ημερίδες',
        'url'   => ($usr['access_level'] == 1) ? 'imerides_list.php':'javascript:void(0)',
        'icon'  => 'fa fa-bullhorn',
        'access'=> 1
    )
);

On Class Users, where it retrieves the access level, i added在 Class Users 上,它检索访问级别,我添加了

if($row['access_level'] != 1){
    unset($primary_nav[1]);
}

How can i do it like, if access level == 1 , remove the array parent ? if access level == 1 ,我该怎么做,删除数组parent

I tried unset($primary_nav[1]) and array_slice but they are not working.我试过unset($primary_nav[1])array_slice但它们不起作用。

抱歉,人们,我只是注意到,我是说,Class Users,它是 Class Users,而 user 是公共函数,而不是传入 $primary_nav,这就是为什么它没有看到要取消设置的数组的原因...

You can do as following:您可以执行以下操作:

$primary_nav = array(
   array(
        'name'  => 'Μέλη',
        'url'   => 'member_list.php',
        'icon'  => 'fa fa-users',
        'access'=> 0
    ),array(
        'name'  => 'Ημερίδες',
        'url'   => 'test',
        'icon'  => 'fa fa-bullhorn',
        'access'=> 1
    )
);

foreach($primary_nav as $index=>$obj){
    if($obj['access']==1){
        unset($primary_nav[$index]);
    }
}

print_r($primary_nav);
Try It ,May Help You .
$primary_nav = array(
   array(
        'name'  => 'Sourabh',
        'url'   => 'member_list.php',
        'icon'  => 'fa fa-users',
        'access'=> 0
    ),array(
        'name'  => 'Nigam',
        'url'   => ($usr['access_level'] == 1) ? 'imerides_list.php':'javascript:void(0)',
        'icon'  => 'fa fa-bullhorn',
        'access'=> 1
    )
);
foreach ($primary_nav as $key=>$primary) {
    foreach ($primary as $vkey=>$value) {
        if($vkey=='access' AND $value==1){
            unset($primary_nav[$key]);
        }
    }
}
echo '<pre>';
print_r($primary_nav);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM