在PHP中设置数组的理论联合 [英] Set Theory Union of arrays in PHP
本文介绍了在PHP中设置数组的理论联合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在数学中有3个带有集合的运算:交集,差和并集(统一).在PHP中,我们可以对数组执行以下操作:
There are 3 operations with sets in mathematics: intersection, difference and union (unification). In PHP we can do this operations with arrays:
- 交叉点:
array_intersect
- 差异:
array_diff
- intersection:
array_intersect
- difference:
array_diff
联合的功能是什么?
结果数组中不能有重复项(如array_intersect
和array_diff
).
No duplicates can be in the result array (like array_intersect
and array_diff
).
如果索引为数字,则array_merge
不会覆盖原始值,而是将其附加( PHP文档).
If indexes are numeric then array_merge
will not overwrite the original value, but will be appended (PHP docs).
推荐答案
尝试array_merge
:
array_unique(array_merge($array1, $array2));
这篇关于在PHP中设置数组的理论联合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文