PHP中数组的集合理论联合 [英] Set Theory Union of arrays in PHP

查看:19
本文介绍了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

union 的作用是什么?

结果数组中不能有重复项(例如 array_intersectarray_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 手册

这篇关于PHP中数组的集合理论联合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆