php 如何从关联数组中删除键及其值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3053517/
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
How can I remove a key and its value from an associative array?
提问by gsquare567
Given an associative array:
给定一个关联数组:
array("key1" => "value1", "key2" => "value2", ...)
How would I go about removing a certain key-value pair, given the key?
给定键,我将如何删除某个键值对?
回答by Sarfraz
You can use unset:
您可以使用unset:
unset($array['key-here']);
Example:
例子:
$array = array("key1" => "value1", "key2" => "value2");
print_r($array);
unset($array['key1']);
print_r($array);
unset($array['key2']);
print_r($array);
Output:
输出:
Array
(
[key1] => value1
[key2] => value2
)
Array
(
[key2] => value2
)
Array
(
)
回答by Bafi
Use this function to remove specific arrays of keys without modifying the original array:
使用此函数删除特定的键数组而不修改原始数组:
function array_except($array, $keys) {
return array_diff_key($array, array_flip((array) $keys));
}
First param pass all array, second param set array of keys to remove.
第一个参数传递所有数组,第二个参数设置要删除的键数组。
For example:
例如:
$array = [
'color' => 'red',
'age' => '130',
'fixed' => true
];
$output = array_except($array, ['color', 'fixed']);
// $output now contains ['age' => '130']
回答by Cristian
Using unset:
使用unset:
unset($array['key1'])
回答by Sahith Vibudhi
Consider this array:
考虑这个数组:
$arr = array("key1" => "value1", "key2" => "value2", "key3" => "value3", "key4" => "value4");
To remove an element using the array
key:// To unset an element from array using Key: unset($arr["key2"]); var_dump($arr); // output: array(3) { ["key1"]=> string(6) "value1" ["key3"]=> string(6) "value3" ["key4"]=> string(6) "value4" }To remove element by
value:// remove an element by value: $arr = array_diff($arr, ["value1"]); var_dump($arr); // output: array(2) { ["key3"]=> string(6) "value3" ["key4"]=> string(6) "value4" }
要使用数组删除元素
key:// To unset an element from array using Key: unset($arr["key2"]); var_dump($arr); // output: array(3) { ["key1"]=> string(6) "value1" ["key3"]=> string(6) "value3" ["key4"]=> string(6) "value4" }通过以下方式删除元素
value:// remove an element by value: $arr = array_diff($arr, ["value1"]); var_dump($arr); // output: array(2) { ["key3"]=> string(6) "value3" ["key4"]=> string(6) "value4" }
read more about array_diff: http://php.net/manual/en/function.array-diff.php
阅读有关 array_diff 的更多信息:http: //php.net/manual/en/function.array-diff.php
To remove an element by using
index:array_splice($arr, 1, 1); var_dump($arr); // array(1) { ["key3"]=> string(6) "value3" }
使用 删除元素
index:array_splice($arr, 1, 1); var_dump($arr); // array(1) { ["key3"]=> string(6) "value3" }
read more about array_splice: http://php.net/manual/en/function.array-splice.php
阅读有关 array_splice 的更多信息:http: //php.net/manual/en/function.array-splice.php
回答by Ruth VBA
You may need two or more loops depending on your array:
根据您的阵列,您可能需要两个或多个循环:
$arr[$key1][$key2][$key3]=$value1; // ....etc
foreach ($arr as $key1 => $values) {
foreach ($key1 as $key2 => $value) {
unset($arr[$key1][$key2]);
}
}

