How to Compare Two or More Arrays in PHP?

03-Apr-2023

.

Admin

How to Compare Two or More Arrays in PHP?

Hi Dev,

This tutorial is focused on how to compare arrays in PHP. you'll learn how to compare two or more arrays in PHP. We will use how to compare arrays using PHP. if you have a question about how to compare two or more arrays using PHP then I will give a simple example with a solution.

Now, let's see the article on how to compare two or more arrays using PHP. it's a simple example of how to compare arrays using PHP. you can understand the concept of how to compare two or more arrays in PHP. if you have a question about how to compare arrays in PHP then I will give a simple example with a solution.

Example 1: Compare Two Arrays in PHP


<?php

//Create the first array

$firstArray = ["first", "second", "third", "fourth", "five"];

//Create a second array

$secondArray = ["first", "second", "third", "fourth"];

//Get the difference between two arrays

$res = array_diff($firstArray, $secondArray);

print_r($res);

?>

Output:

Array (

[4] => five

)

Example 2: Compare Numeric Array in PHP

<?php

//Create the first array

$firstArray = [1, 2, 3, 4, 5];

//Create a second array

$secondArray = [1, 2, 3, 4];

//Get the difference between two arrays

$res = array_diff($firstArray, $secondArray);

print_r($res);

?>

Output:

Array (

[4] => 5

)

I hope it could help you...

#PHP