下面简单介绍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()函数的更多相关文章

  1. PHP比较两个数组的差异

    array_diff($arr, $arr1); //比较数组差异 $arr = [1,2,3,4]; $arr1 = [1,2,3]; $diff = array_diff($arr, $arr1) ...

  2. c#比较两个数组的差异

    将DataTable中某一列数据直接转换成数组进行比较,使用的Linq,要引用命名空间using System.Linq; string[] arrRate = dtRate.AsEnumerable ...

  3. leetcode 349:两个数组的交集I

    Problem: Given two arrays, write a function to compute their intersection. 中文:已知两个数组,写一个函数来计算它们的交集 E ...

  4. [Swift]LeetCode349. 两个数组的交集 | Intersection of Two Arrays

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  5. 6、两个数组的交集 II

    6.两个数组的交集 II 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入: n ...

  6. Leetcode 350.两个数组的交集|| By Python

    给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入: nums1 = [4,9,5 ...

  7. Leetcode 349. 两个数组的交集 By Python

    给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2] 示例 2: 输入: nums1 = [4,9,5], ...

  8. python(leetcode)-350两个数组的交集

    给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入: nums1 = [4,9,5 ...

  9. 初级算法-6.两个数组的交集 II

    题目描述: 给定两个数组,编写一个函数来计算它们的交集. 示例 : 输入: nums1 = [,,,], nums2 = [,] 输出: [,] 示例 : 输入: nums1 = [,,], nums ...

随机推荐

  1. B+/-Tree原理(mysql索引数据结构)

    B+/-Tree原理 B-Tree介绍 B-Tree是一种多路搜索树(并不是二叉的):       1.定义任意非叶子结点最多只有M个儿子:且M>2:       2.根结点的儿子数为[2, M ...

  2. 【Leetcode】【Medium】Subsets

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  3. June 23rd 2017 Week 25th Friday

    Life doesn't get easier, you just get stronger. 生活从未变得轻松,是你在一点一点变得坚强. So in the same way we can get ...

  4. html5 css3新特性了解一下

    html5: 用于绘画的 canvas 元素 以及SVG 用于媒介回放的 video 和 audio 元素 拖拽(Drag 和 drop) 地理定位(Geolocation) 对本地离线存储的更好的支 ...

  5. api的使用机制:继承、实例化、实现(继承)配置、实例(参数化)配置、机制管理模块

    api的使用机制:继承.实例化.实现(继承)配置.实例(参数化)配置.机制管理模块 facade模式.管理模块

  6. webpack究竟是什么

    在很久很久以前,当我们写一个web网页的时候,js实现的逻辑相对是比较弱的.但随着前端技术的发展,前端能实现的内容越来越多.在js里面加了非常非常多的逻辑,于是呢,我们就发现我们通过这种面向过程的方式 ...

  7. fastcgi c/c++ API 说明

    fastcgi c/c++ API 下载地址:https://github.com/FastCGI-Archives 先上example #include <iostream> #incl ...

  8. TCP建立连接和释放连接过程

    TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的.可靠的.基于字节流的传输层通信协议.TCP建立连接需要三次握手,释放连接需要四次握手. 1.TCP整 ...

  9. mac git 命令自动补全

    步骤如下: 1.下载Git-completion.bash 或者直接使用SourceTree去clone到本地. 下载地址:https://github.com/markgandolfo/git-ba ...

  10. Intellij IDEA如何删除项目

    1.      删除某个项目 1)       选中项目,并关闭 2)       File-Open,再选中相应的项目,并delete即可,它会同时在硬盘上删除