php 按值删除数组项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1883421/
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
Removing array item by value
提问by Marek
I need to remove array item with given value:
我需要删除具有给定值的数组项:
if (in_array($id, $items)) {
$items = array_flip($items);
unset($items[ $id ]);
$items = array_flip($items);
}
Could it be done in shorter (more efficient) way?
能否以更短(更有效)的方式完成?
回答by Alejandro García Iglesias
It can be accomplished with a simple one-liner.
它可以通过一个简单的单线完成。
Having this array:
有这个数组:
$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 the value of $arrwill be:
而$arrwill的值是:
array('nice_item', 'another_liked_item')
Hope it helps write beautiful code.
希望它有助于编写漂亮的代码。
回答by zombat
I am adding a second answer. I wrote a quick benchmarking script to try various methods here.
我正在添加第二个答案。我写了一个快速的基准测试脚本来尝试这里的各种方法。
$arr = array(0 => 123456);
for($i = 1; $i < 500000; $i++) {
$arr[$i] = rand(0,PHP_INT_MAX);
}
shuffle($arr);
$arr2 = $arr;
$arr3 = $arr;
/**
* Method 1 - array_search()
*/
$start = microtime(true);
while(($key = array_search(123456,$arr)) !== false) {
unset($arr[$key]);
}
echo count($arr). ' left, in '.(microtime(true) - $start).' seconds<BR>';
/**
* Method 2 - basic loop
*/
$start = microtime(true);
foreach($arr2 as $k => $v) {
if ($v == 123456) {
unset($arr2[$k]);
}
}
echo count($arr2). 'left, in '.(microtime(true) - $start).' seconds<BR>';
/**
* Method 3 - array_keys() with search parameter
*/
$start = microtime(true);
$keys = array_keys($arr3,123456);
foreach($keys as $k) {
unset($arr3[$k]);
}
echo count($arr3). 'left, in '.(microtime(true) - $start).' seconds<BR>';
The third method, array_keys()with the optional search parameter specified, seems to be by far the best method. Output example:
第三种方法,array_keys()指定了可选的搜索参数,似乎是迄今为止最好的方法。输出示例:
499999 left, in 0.090957164764404 seconds
499999left, in 0.43156313896179 seconds
499999left, in 0.028877019882202 seconds
Judging by this, the solution I would use then would be:
据此判断,我将使用的解决方案是:
$keysToRemove = array_keys($items,$id);
foreach($keysToRemove as $k) {
unset($items[$k]);
}
回答by zombat
How about:
怎么样:
if (($key = array_search($id, $items)) !== false) unset($items[$key]);
or for multiple values:
或多个值:
while(($key = array_search($id, $items)) !== false) {
unset($items[$key]);
}
This would prevent key loss as well, which is a side effect of array_flip().
这也可以防止密钥丢失,这是array_flip().
回答by Toma
to remove $rm_valfrom $arr
除去$rm_val从$arr
unset($arr[array_search($rm_val, $arr)]);
回答by Pascal MARTIN
The most powerful solution would be using array_filter, which allows you to define your own filtering function.
最强大的解决方案是使用array_filter,它允许您定义自己的过滤功能。
But some might say it's a bit overkill, in your situation...
A simple foreachloop to go trough the array and remove the item you don't want should be enough.
但有些人可能会说,在您的情况下,这有点矫枉过正……
一个简单的foreach循环遍历数组并删除您不想要的项目就足够了。
Something like this, in your case, should probably do the trick :
像这样的事情,在你的情况下,应该可以解决问题:
foreach ($items as $key => $value) {
if ($value == $id) {
unset($items[$key]);
// If you know you only have one line to remove, you can decomment the next line, to stop looping
//break;
}
}
回答by Dutow
Your solutions only work if you have unique values in your array
只有在数组中有唯一值时,您的解决方案才有效
See:
看:
<?php
$trans = array("a" => 1, "b" => 1, "c" => 2);
$trans = array_flip($trans);
print_r($trans);
?>
A better way would be unset with array_search, in a loop if neccessary.
如果需要,可以在循环中使用array_search取消设置更好的方法。
回答by Savageman
回答by erenon
w/o flip:
无翻转:
<?php
foreach ($items as $key => $value) {
if ($id === $value) {
unset($items[$key]);
}
}
回答by Suman Biswas
function deleteValyeFromArray($array,$value)
{
foreach($array as $key=>$val)
{
if($val == $value)
{
unset($array[$key]);
}
}
return $array;
}
回答by Tejas Soni
You can use array_splice function for this operation Ref : array_splice
您可以为此操作使用 array_splice 函数参考:array_splice
array_splice($array, array_search(58, $array ), 1);

