如何从PHP中删除数组中的值?

时间:2021-12-11 13:41:36

Is there a PHP function to remove certain array elements from an array?

是否有PHP函数从数组中删除某些数组元素?

E.g., I have an array (A) with values and another array (B) from which a need to remove values.

例如,我有一个带有值的数组(A)和另一个需要删除值的数组(B)。

Want to remove the values in array A from array B?

想要从数组B中删除数组A中的值吗?

6 个解决方案

#1


31  

Use array_diff()

使用array_diff()

$new_array = array_diff($arrayB, $arrayA);

will return an array with all the elements from $arrayB that are not in $arrayA.

将返回一个数组,其中包含$ arrayB中不在$ arrayA中的所有元素。

To do this with associative arrays use array_diff_assoc().

要使用关联数组执行此操作,请使用array_diff_assoc()。

To remove a single value use:

要删除单个值,请使用:

unset($array[$key]);

You can of course loop that to do the equivalent of the array functions but there's no point in that.

你当然可以循环它来做相当于数组函数但没有任何意义。

#2


5  

It depends on what you mean by "remove".

这取决于你的意思是“删除”。

You can use the unset() function to remove keys from your array, but this will not reindex it. So for example, if you have:

您可以使用unset()函数从数组中删除键,但这不会重新索引它。例如,如果你有:

$a = array(1 => 'one', 2 => 'two', 3 => 'three');

and you then call

然后你打电话

unset($a[2]);

You'll end up with something like

你最终会得到类似的东西

(1 => 'one', 3 => 'three');

If you need the array to be sequentially indexed, you can take the unsetted array and feed it into array_values(), which will return a new array with sequentially indexed keys.

如果需要对数组进行顺序索引,则可以使用未设置的数组并将其提供给array_values(),这将返回带有顺序索引键的新数组。

Returning to your original scenario, as others observe, array_diff will do the job for you, but note that it doesn't do an index check. If you need that, use array_diff_assoc, instead.

回到原来的场景,正如其他人观察到的那样,array_diff会为你完成这项工作,但请注意它不会进行索引检查。如果需要,请使用array_diff_assoc。

#3


2  

The array_diff function will do this for you.

array_diff函数将为您执行此操作。

#4


1  

$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);

#5


1  

I came accros this post looking for a way to remove one single value from an array (which the title states). Here is a straight forward way, assuming the value to remove is in the array:

我来到accros这篇文章寻找一种方法从数组中删除一个单独的值(标题所述)。这是一种直接的方式,假设要删除的值在数组中:

$list = array('foo', 'bar', 'yay', '\o/');
$toremove = 'foo';
unset($list[array_search($toremove, $list)]);

Though this will throw errors if the element to remove is not part of the array.

虽然如果要删除的元素不是数组的一部分,这会抛出错误。

Another solution, but not really optimised performance wise is:

另一个解决方案,但没有真正优化性能明智的是:

$list = array('foo', 'bar', 'yay', '\o/');
$toremove = 'foo';
$list = array_flip($list);
unset($list[$toremove]);
$list = array_flip($list);

Anyway, perhaps creating an array with the single value as using array_diff as suggested by everyone here is quicker and more efficient.

无论如何,也许创建一个具有单个值的数组,就像所有人在这里建议的使用array_diff一样,更快更有效。

#6


1  

Perhaps a simple and quicker solution is this. A simple reiteration that can delete(unset) 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]);
    }

#1


31  

Use array_diff()

使用array_diff()

$new_array = array_diff($arrayB, $arrayA);

will return an array with all the elements from $arrayB that are not in $arrayA.

将返回一个数组,其中包含$ arrayB中不在$ arrayA中的所有元素。

To do this with associative arrays use array_diff_assoc().

要使用关联数组执行此操作,请使用array_diff_assoc()。

To remove a single value use:

要删除单个值,请使用:

unset($array[$key]);

You can of course loop that to do the equivalent of the array functions but there's no point in that.

你当然可以循环它来做相当于数组函数但没有任何意义。

#2


5  

It depends on what you mean by "remove".

这取决于你的意思是“删除”。

You can use the unset() function to remove keys from your array, but this will not reindex it. So for example, if you have:

您可以使用unset()函数从数组中删除键,但这不会重新索引它。例如,如果你有:

$a = array(1 => 'one', 2 => 'two', 3 => 'three');

and you then call

然后你打电话

unset($a[2]);

You'll end up with something like

你最终会得到类似的东西

(1 => 'one', 3 => 'three');

If you need the array to be sequentially indexed, you can take the unsetted array and feed it into array_values(), which will return a new array with sequentially indexed keys.

如果需要对数组进行顺序索引,则可以使用未设置的数组并将其提供给array_values(),这将返回带有顺序索引键的新数组。

Returning to your original scenario, as others observe, array_diff will do the job for you, but note that it doesn't do an index check. If you need that, use array_diff_assoc, instead.

回到原来的场景,正如其他人观察到的那样,array_diff会为你完成这项工作,但请注意它不会进行索引检查。如果需要,请使用array_diff_assoc。

#3


2  

The array_diff function will do this for you.

array_diff函数将为您执行此操作。

#4


1  

$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);

#5


1  

I came accros this post looking for a way to remove one single value from an array (which the title states). Here is a straight forward way, assuming the value to remove is in the array:

我来到accros这篇文章寻找一种方法从数组中删除一个单独的值(标题所述)。这是一种直接的方式,假设要删除的值在数组中:

$list = array('foo', 'bar', 'yay', '\o/');
$toremove = 'foo';
unset($list[array_search($toremove, $list)]);

Though this will throw errors if the element to remove is not part of the array.

虽然如果要删除的元素不是数组的一部分,这会抛出错误。

Another solution, but not really optimised performance wise is:

另一个解决方案,但没有真正优化性能明智的是:

$list = array('foo', 'bar', 'yay', '\o/');
$toremove = 'foo';
$list = array_flip($list);
unset($list[$toremove]);
$list = array_flip($list);

Anyway, perhaps creating an array with the single value as using array_diff as suggested by everyone here is quicker and more efficient.

无论如何,也许创建一个具有单个值的数组,就像所有人在这里建议的使用array_diff一样,更快更有效。

#6


1  

Perhaps a simple and quicker solution is this. A simple reiteration that can delete(unset) 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]);
    }