简体   繁体   中英

How to unset an array element matching index in php?

$menu contains:

Array
    (
        [0] => Array
            (
                [menu] => Array
                    (
                        [name] => Home
                        [controller] => frontends
                        [action] => index
                    )

            )

        [1] => Array
            (
                [menu] => Array
                    (
                        [name] => Feedback
                        [controller] => feedbacks
                        [action] => add
                    )

            )

        [2] => Array
            (
                [menu] => Array
                    (
                        [name] => Reseller
                        [controller] => resellers
                        [action] => login
                    )

            )

    )

I want to delete

[2] => Array
        (
            [menu] => Array
                (
                    [name] => Reseller
                    [controller] => resellers
                    [action] => login
                )

        )

unset($menu[2])

Works fine. But I am not sure that this menu always under 2 index. So I want to delete this item when $menu[$i][menu][name] == 'Reseller' . Anyone able to help?

Did not test this, but this should work.

foreach ($menu as $index => $menu_item) {
    if ($menu_item['menu']['name'] == 'Reseller') {
        unset($menu[$index]);
    }
}

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