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

How to compare the difference between two arrays in php

angryTom
Release: 2023-04-08 10:58:01
forward
4846 people have browsed it

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)
}
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template