php 如何根据键删除数组元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1672156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:27:52  来源:igfitidea点击:

How to delete an array element based on key?

phparrays

提问by Bruce Dou

Possible Duplicate:
How to delete an element from an array in php?

可能的重复:
如何从 php 中的数组中删除一个元素?

For instance,

例如,

Array(      
    [0] => Array
        (
            [0] => hello
            [1] => open
        )

    [1] => Array
        (
            [0] => good
            [1] => center
        )

    [2] => Array
        (
            [0] => hello
            [1] => close
        )
)

I want to delete the element which key is 1, after the operation:

我想在操作后删除 key 为1的元素:

Array(
    [0] => Array
        (
            [0] => hello
            [1] => open
        )

    [2] => Array
        (
            [0] => hello
            [1] => close
        )
)

回答by David Kurid?a

PHP

PHP

unset($array[1]);

回答by cletus

You don't say what language you're using, but looking at that output, it looks like PHP output (from print_r()).
If so, just use unset():

您没有说明您使用的是哪种语言,但查看该输出,它看起来像 PHP 输出(来自print_r())。
如果是这样,只需使用unset()

unset($arr[1]);

回答by mauris

this looks like PHP to me. I'll delete if it's some other language.

这对我来说就像 PHP。如果是其他语言,我会删除。

Simply unset($arr[1]);

简单地 unset($arr[1]);