PHP:如何从数组中删除特定元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2448964/
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: How to remove specific element from an array?
提问by dcp3450
How do I remove an element from an array when I know the elements name? for example:
当我知道元素名称时,如何从数组中删除元素?例如:
I have an array:
我有一个数组:
$array = array('apple', 'orange', 'strawberry', 'blueberry', 'kiwi');
the user enters strawberry
用户输入 strawberry
strawberryis removed.
strawberry已移除。
To fully explain:
全面解释:
I have a database that stores a list of items separated by a comma. The code pulls in the list based on a user choice where that choice is located. So, if they choose strawberry they code pulls in every entry were strawberry is located then converts that to an array using split(). I want to them remove the user chosen items, for this example strawberry, from the array.
我有一个数据库,用于存储以逗号分隔的项目列表。该代码根据该选项所在的用户选择拉入列表。因此,如果他们选择草莓,他们的代码会在草莓所在的每个条目中提取代码,然后使用 split() 将其转换为数组。我希望他们从数组中删除用户选择的项目,例如草莓。
回答by Gumbo
Use array_searchto get the key and remove it with unsetif found:
使用array_search拿到钥匙,并删除它unset一旦发现:
if (($key = array_search('strawberry', $array)) !== false) {
unset($array[$key]);
}
array_searchreturns false(nulluntil PHP 4.2.0) if no item has been found.
array_search如果未找到任何项目,则返回false(在 PHP 4.2.0 之前为null)。
And if there can be multiple items with the same value, you can use array_keysto get the keys to all items:
如果可以有多个具有相同值的项目,您可以使用array_keys获取所有项目的键:
foreach (array_keys($array, 'strawberry') as $key) {
unset($array[$key]);
}
回答by ills
Use array_diff()for 1 line solution:
使用array_diff()1级的解决方案:
$array = array('apple', 'orange', 'strawberry', 'blueberry', 'kiwi', 'strawberry'); //throw in another 'strawberry' to demonstrate that it removes multiple instances of the string
$array_without_strawberries = array_diff($array, array('strawberry'));
print_r($array_without_strawberries);
...No need for extra functions or foreach loop.
...不需要额外的函数或 foreach 循环。
回答by John Conde
if (in_array('strawberry', $array))
{
unset($array[array_search('strawberry',$array)]);
}
回答by ericluwj
If you are using a plain array here (which seems like the case), you should be using this code instead:
如果您在此处使用普通数组(似乎是这种情况),则应改用以下代码:
if (($key = array_search('strawberry', $array)) !== false) {
array_splice($array, $key, 1);
}
unset($array[$key])only removes the element but does not reorder the plain array.
unset($array[$key])只删除元素但不重新排序普通数组。
Supposingly we have an array and use array_splice:
假设我们有一个数组并使用 array_splice:
$array = array('apple', 'orange', 'strawberry', 'blueberry', 'kiwi');
array_splice($array, 2, 1);
json_encode($array);
// yields the array ['apple', 'orange', 'blueberry', 'kiwi']
Compared to unset:
与未设置相比:
$array = array('apple', 'orange', 'strawberry', 'blueberry', 'kiwi');
unset($array[2]);
json_encode($array);
// yields an object {"0": "apple", "1": "orange", "3": "blueberry", "4": "kiwi"}
Notice how unset($array[$key])does not reorder the array.
请注意如何unset($array[$key])不对数组重新排序。
回答by srcspider
You can use array filter to remove the items by a specific condition on $v:
您可以使用数组过滤器按特定条件删除项目$v:
$arr = array_filter($arr, function($v){
return $v != 'some_value';
});
回答by D.Martin
Will be like this:
会是这样:
function rmv_val($var)
{
return(!($var == 'strawberry'));
}
$array = array('apple', 'orange', 'strawberry', 'blueberry', 'kiwi');
$array_res = array_filter($array, "rmv_val");
回答by mmr
This is a simple reiteration that can delete multiple values in the array.
这是一个简单的重复,可以删除数组中的多个值。
// Your array
$list = array("apple", "orange", "strawberry", "lemon", "banana");
// Initilize what to delete
$delete_val = array("orange", "lemon", "banana");
// Search for the array key and unset
foreach($delete_val as $key){
$keyToDelete = array_search($key, $list);
unset($list[$keyToDelete]);
}
回答by jankal
I'm currently using this function:
我目前正在使用这个功能:
function array_delete($del_val, $array) {
if(is_array($del_val)) {
foreach ($del_val as $del_key => $del_value) {
foreach ($array as $key => $value){
if ($value == $del_value) {
unset($array[$key]);
}
}
}
} else {
foreach ($array as $key => $value){
if ($value == $del_val) {
unset($array[$key]);
}
}
}
return array_values($array);
}
You can input an array or only a string with the element(s) which should be removed. Write it like this:
您可以输入一个数组或只输入一个包含应删除元素的字符串。像这样写:
$detils = array('apple', 'orange', 'strawberry', 'blueberry', 'kiwi');
$detils = array_delete(array('orange', 'apple'), $detils);
OR
或者
$detils = array_delete('orange', $detils);
$detils = array_delete('orange', $detils);
It'll also reindex it.
它也会重新索引它。
回答by d?lo sürücü
Just u can do single line .it will be remove element from array
只是你可以做单行。它将从数组中删除元素
$array=array_diff($array,['strawberry']);
回答by Jordan Montel
This question has several answers but I want to add something more because when I used unsetor array_diffI had several problems to play with the indexes of the new array when the specific element was removed (because the initial index are saved)
这个问题有几个答案,但我想添加更多内容,因为当我使用unset或array_diff删除特定元素时我在处理新数组的索引时遇到了几个问题(因为保存了初始索引)
I get back to the example :
我回到这个例子:
$array = array('apple', 'orange', 'strawberry', 'blueberry', 'kiwi');
$array_without_strawberries = array_diff($array, array('strawberry'));
or
或者
$array = array('apple', 'orange', 'strawberry', 'blueberry', 'kiwi');
unset($array[array_search('strawberry', $array)]);
If you print the result you will obtain :
如果打印结果,您将获得:
foreach ($array_without_strawberries as $data) {
print_r($data);
}
Result :
结果 :
> apple
> orange
> blueberry
> kiwi
But the indexes will be saved and so you will access to your element like :
但是索引将被保存,因此您可以访问您的元素,例如:
$array_without_strawberries[0] > apple
$array_without_strawberries[1] > orange
$array_without_strawberries[3] > blueberry
$array_without_strawberries[4] > kiwi
And so the final array are not re-indexed. So you need to add after the unsetor array_diff:
所以最终的数组不会被重新索引。所以你需要在unsetor之后添加array_diff:
$array_without_strawberries = array_values($array);
After that your array will have a normal index :
之后,您的数组将具有正常索引:
$array_without_strawberries[0] > apple
$array_without_strawberries[1] > orange
$array_without_strawberries[2] > blueberry
$array_without_strawberries[3] > kiwi
Related to this post : Re-Index Array
与这篇文章相关:重新索引数组
Hope it will help
希望它会有所帮助


