Home  >  Article  >  Backend Development  >  How to compare the difference between two arrays in php

How to compare the difference between two arrays in php

angryTom
angryTomforward
2020-01-27 21:40:234727browse

During development, you may encounter the need to compare the difference between two arrays. So how to use PHP to achieve this? In fact, PHP provides us with such a function: array_diff. Let’s take a look at how to use it.

How to compare the difference between two arrays in php

Commonly used PHP compares the difference between two arrays

array_diff($arr, $arr1);

//比较数组差异
$arr = [1,2,3,4];
 
$arr1 = [1,2,3];
$diff = array_diff($arr, $arr1);
dump($diff);
 
//打印输出如下,函数第二个参数为参数值,没有差异则返回空数组
array(1) {
  [3] => int(4)
}

This article comes from the php tutorial column, welcome to learn.

The above is the detailed content of How to compare the difference between two arrays in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete