How to compare two arrays in php to see if they are different

青灯夜游
Release: 2023-03-15 18:26:01
Original
3771 people have browsed it

Comparison method: 1. Use "array_diff(array 1, array 2)" to compare only array values; 2. Use "array_diff_assoc(array 1, array 2)" to compare both keys and values. After comparing the arrays, these two methods return a difference array; if the difference array is an empty array, the two arrays are the same, and vice versa.

How to compare two arrays in php to see if they are different

The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer

php comparison 2 Whether the arrays are different (not the same)

In PHP, you can use the array_diff() or array_diff_assoc() function to compare two arrays to see if the two arrays are different.

The array_diff() or array_diff_assoc() function will return a difference array after comparing arrays; if the difference array is an empty array, the two arrays are the same, and vice versa.

Let’s take a closer look:

Method 1: Use array_diff() to compare whether two arrays are different

array_diff() function is used Compares the values ​​of two arrays and returns the difference. Syntax format:

array_diff(array1,array2);
Copy after login

Return value:

  • Returns a difference array that includes everything in the compared array (array1), but not in any other Values ​​in parameter arrays (array2, etc.).

Example:

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"black","g"=>"purple");
$a3=array("a"=>"red","b"=>"black","h"=>"purple");

$result=array_diff($a1,$a2);
var_dump($result);

$result=array_diff($a2,$a3);
var_dump($result);
?>
Copy after login

How to compare two arrays in php to see if they are different

Method 2: Use array_diff_assoc() function

The array_diff_assoc() function is used to compare the key names and key values ​​of two (or more) arrays and return the difference. Syntax format:

array_diff_assoc(array1,array2);
Copy after login

This function compares the key names and key values ​​​​of two (or more) arrays, and returns a difference array, which includes all the arrays being compared (array1), But the key name and key value are not in any other parameter array (array2).

Example:

<?php
$a1=array("a"=>"red","b"=>"black","g"=>"purple");
$a2=array("e"=>"red","f"=>"black","g"=>"purple");
$a3=array("a"=>"red","b"=>"blue","h"=>"yellow");
$a4=array("e"=>"red","f"=>"black","g"=>"purple");

$result=array_diff_assoc($a1,$a2);
var_dump($result);

$result=array_diff_assoc($a2,$a3);
var_dump($result);

$result=array_diff_assoc($a2,$a4);
var_dump($result);
?>
Copy after login

How to compare two arrays in php to see if they are different

Recommended learning: "PHP Video Tutorial"

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

Related labels:
source:php.cn
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