简体   繁体   中英

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 ..

$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

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

How can i do it like, if access level == 1 , remove the array parent ?

I tried unset($primary_nav[1]) and array_slice but they are not working.

抱歉,人们,我只是注意到,我是说,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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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