php比较两个数组的差异array_diff()函数
下面简单介绍php比较两个数组的差异array_diff()函数。
原文地址:小时刻个人技术博客 > http://small.aiweimeng.top/index.php/archives/9.html
php内置函数中有一个array_diff():意思比较两个数组的键值,并返回差集。
下面是通过数组遍历来实现差集:
首先有两个不同的数组$arr_1,$arr_2:
一种方法:
function fun($arr_1,$arr_2)
{
foreach ($arr_1 as $key => $val)
{
$bool = false;
foreach ($arr_2 as $k => $v)
{
if($bool = ($val == $v))
{
break;
}
}
if(!$bool)
{
$diff[$key] = $val;
}
}
}
这种虽然能够实现数组间的差异,如果数组长度达到一定的长度后,效率不容乐观。
二种方法:
foreach ($arr_1 as $key => $val)
{
if(in_array($val,$arr_2))
{
unset($arr_1[$key]);
}
}
这种方式在其他博客上说可以和array_diff()媲美。
三种方式:
通过```array_flip();```将arr_2数组键值反转后,判断arr_1数组中的值是否在arr_2的键中
$arr_2 = array_flip($arr_2);
foreach ($arr_1 as $key => $val)
{
if(isset($arr_2[$val]))
{
unset($arr_1[$key]);
}
}
php比较两个数组的差异array_diff()函数的更多相关文章
- PHP比较两个数组的差异
array_diff($arr, $arr1); //比较数组差异 $arr = [1,2,3,4]; $arr1 = [1,2,3]; $diff = array_diff($arr, $arr1) ...
- c#比较两个数组的差异
将DataTable中某一列数据直接转换成数组进行比较,使用的Linq,要引用命名空间using System.Linq; string[] arrRate = dtRate.AsEnumerable ...
- leetcode 349:两个数组的交集I
Problem: Given two arrays, write a function to compute their intersection. 中文:已知两个数组,写一个函数来计算它们的交集 E ...
- [Swift]LeetCode349. 两个数组的交集 | Intersection of Two Arrays
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
- 6、两个数组的交集 II
6.两个数组的交集 II 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入: n ...
- Leetcode 350.两个数组的交集|| By Python
给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入: nums1 = [4,9,5 ...
- Leetcode 349. 两个数组的交集 By Python
给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2] 示例 2: 输入: nums1 = [4,9,5], ...
- python(leetcode)-350两个数组的交集
给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入: nums1 = [4,9,5 ...
- 初级算法-6.两个数组的交集 II
题目描述: 给定两个数组,编写一个函数来计算它们的交集. 示例 : 输入: nums1 = [,,,], nums2 = [,] 输出: [,] 示例 : 输入: nums1 = [,,], nums ...
随机推荐
- 面试准备之三Django知识
Django请求流程 MTV模式 路由 视图 ORM 模板
- C# 导出excel文件处理科学计数法办法
在邦定gridview控件时在rowdatabound事件中队数据格式化 protected void DataGridView1_RowDataBound(object sender, GridVi ...
- angular中的scope
angular.element($0).scope() 什么是scope? scope是一个refer to the application model的object.它是一个expression的执 ...
- golang构造函数
http://blog.jobbole.com/107442/?utm_source=blog.jobbole.com&utm_medium=relatedPosts https://gocn ...
- SQL Server ->> 时间函数: EOMONTH, DATEFROMPARTS, TIMEFROMPARTS, DATETIMEFROMPARTS, DATETIMEOFFSETFROMPARTS
上面几个函数都是SQL Server 2012新增的时间函数. EOMONTH 返回传入时间的月结束日,返回数据类型为DATE SELECT EOMONTH(GETDATE()) 结果为 DATEFR ...
- C++中虚继承的作用及底层实现原理
http://blog.csdn.net/bxw1992/article/details/77726390
- Windows Server 2012/2012 R2:安装和配置 SMTP 服务器
Windows Server 2012/2012 R2:安装和配置 SMTP 服务器 安装 SMTP 服务器 以下是安装 SMTP 服务器功能的步骤: 打开“服务器管理器”:单击键盘上的 Window ...
- 全新释放 | RealSight APM, 让客户的极致数字体验成为可能
根据专业评测机构 downdetector.com 统计,2018年,Facebook 系统全年宕机 200 次,Youtube 宕机 140 次,Google 宕机 100 次.每次宕机损失至少 ...
- java8的4大核心函数式接口
//java8的4大核心函数式接口//1.Consumer<T>:消费性接口//需求:public void happy(double money, Consumer<Double& ...
- 使用SuperMap Deskpro 6R.NET生成地图缓存
使用说明 “地图缓存”按钮,用来对二维地图数据生成缓存文件,优化地图的浏览效果. 操作步骤 1.单击功能区>“数据”选项卡>“生成缓存”组中的“地图缓存”按钮. 2.弹出如下所示“生成地图 ...