小编典典

如何从关联数组中删除键及其值?

all

给定一个关联数组:

array("key1" => "value1", "key2" => "value2", ...)

给定键,我将如何删除某个键值对?


阅读 69

收藏
2022-06-22

共1个答案

小编典典

您可以使用unset

unset($array['key-here']);

例子:

$array = array("key1" => "value1", "key2" => "value2");
print_r($array);

unset($array['key1']);
print_r($array);

unset($array['key2']);
print_r($array);

输出:

Array
(
    [key1] => value1
    [key2] => value2
)
Array
(
    [key2] => value2
)
Array
(
)
2022-06-22