PHP 数组按值删除(非键)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7225070/
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
PHP array delete by value (not key)
提问by Adam Strudwick
I have a PHP array as follows:
我有一个 PHP 数组,如下所示:
$messages = [312, 401, 1599, 3, ...];
I want to delete the element containing the value $del_val
(for example, $del_val=401
), but I don't know its key. This might help: each value can only be there once.
我想删除包含值的元素$del_val
(例如,$del_val=401
),但我不知道它的键。这可能会有所帮助:每个值只能出现一次。
I'm looking for the simplest function to perform this task, please.
我正在寻找执行此任务的最简单的函数。
回答by Bojangles
Using array_search()
and unset
, try the following:
使用array_search()
和unset
,尝试以下操作:
if (($key = array_search($del_val, $messages)) !== false) {
unset($messages[$key]);
}
array_search()
returns the key of the element it finds, which can be used to remove that element from the original array using unset()
. It will return FALSE
on failure, however it can return a false-y value on success (your key may be 0
for example), which is why the strict comparison !==
operator is used.
array_search()
返回它找到的元素的键,它可用于使用unset()
. 它会FALSE
在失败时返回,但是它可以在成功时返回 false-y 值(0
例如,您的密钥可能是),这就是使用严格比较运算符的原因!==
。
The if()
statement will check whether array_search()
returned a value, and will only perform an action if it did.
该if()
语句将检查是否array_search()
返回了一个值,并且只有在返回值时才会执行操作。
回答by Rok Kralj
Well, deleting an element from array is basically just set differencewith one element.
好吧,从数组中删除一个元素基本上只是与一个元素设置差异。
array_diff( [312, 401, 15, 401, 3], [401] ) // removing 401 returns [312, 15, 3]
It generalizes nicely, you can remove as many elements as you like at the same time, if you want.
它很好地概括,如果需要,您可以同时删除任意数量的元素。
Disclaimer:Note that my solution produces a new copy of the array while keeping the old one intact in contrast to the accepted answer which mutates. Pick the one you need.
免责声明:请注意,与已接受的变异答案相比,我的解决方案会生成数组的新副本,同时保持旧的完整副本。选择您需要的那个。
回答by Ja?ck
One interesting way is by using array_keys()
:
一种有趣的方法是使用array_keys()
:
foreach (array_keys($messages, 401, true) as $key) {
unset($messages[$key]);
}
The array_keys()
function takes two additional parameters to return only keys for a particular value and whether strict checking is required (i.e. using === for comparison).
该array_keys()
函数需要两个额外的参数来仅返回特定值的键以及是否需要严格检查(即使用 === 进行比较)。
This can also remove multiple array items with the same value (e.g. [1, 2, 3, 3, 4]
).
这也可以删除具有相同值的多个数组项(例如[1, 2, 3, 3, 4]
)。
回答by adlawson
If you know for definite that your array will contain only one element with that value, you can do
如果你确定你的数组将只包含一个具有该值的元素,你可以这样做
$key = array_search($del_val, $array);
if (false !== $key) {
unset($array[$key]);
}
If, however, your value might occur more than once in your array, you could do this
但是,如果您的值在您的数组中可能出现不止一次,您可以这样做
$array = array_filter($array, function($e) use ($del_val) {
return ($e !== $del_val);
});
Note:The second option only works for PHP5.3+ with Closures
注意:第二个选项仅适用于带闭包的PHP5.3+
回答by Rmannn
$fields = array_flip($fields);
unset($fields['myvalue']);
$fields = array_flip($fields);
回答by theCodeMachine
Have a look at following code:
看看下面的代码:
$arr = array('nice_item', 'remove_me', 'another_liked_item', 'remove_me_also');
You can do:
你可以做:
$arr = array_diff($arr, array('remove_me', 'remove_me_also'));
And that will get you this array:
这会给你这个数组:
array('nice_item', 'another_liked_item')
回答by Airy
The Best way is array_splice
最好的办法是 array_splice
array_splice($array, array_search(58, $array ), 1);
Reason for Best is here at http://www.programmerinterview.com/index.php/php-questions/how-to-delete-an-element-from-an-array-in-php/
回答by Victor Priceputu
Or simply, manual way:
或者简单地说,手动方式:
foreach ($array as $key => $value){
if ($value == $target_value) {
unset($array[$key]);
}
}
This is the safest of them because you have full control on your array
这是其中最安全的,因为您可以完全控制您的阵列
回答by David Lin
If you have PHP 5.3+
, there is the one line code:
如果您有 PHP 5.3+
,则有一行代码:
$array = array_filter($array, function ($i) use ($value) { return $i !== $value; });
回答by Syed Abidur Rahman
By the following code, the repetitive values will be removed from the $messages.
通过以下代码,将从 $messages 中删除重复值。
$messages = array_diff($messages, array(401));
$messages = array_diff($messages, array(401));