如何从PHP中的数组中删除值? [英] How to remove values from an array in PHP?
本文介绍了如何从PHP中的数组中删除值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有PHP函数可以从数组中删除某些数组元素?
Is there a PHP function to remove certain array elements from an array?
例如,我有一个带有值的数组(A
)和另一个需要从中删除值的数组(B
).
E.g., I have an array (A
) with values and another array (B
) from which a need to remove values.
是否要从数组B
中删除数组A
中的值?
Want to remove the values in array A
from array B
?
推荐答案
使用 array_diff()
$new_array = array_diff($arrayB, $arrayA);
将返回一个数组,其中包含$arrayB
中所有不是的$arrayB
元素.
will return an array with all the elements from $arrayB
that are not in $arrayA
.
要对关联数组执行此操作,请使用 array_diff_assoc()
.
To do this with associative arrays use 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.
这篇关于如何从PHP中的数组中删除值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文