Skip to content

All topics  /  PHP

How to Compare Two or More Arrays in PHP?

PHP ·

Teams applying “How to Compare Two or More Arrays in PHP?” in a production project can use the related Monitask article to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.

Before copying the example into a live application, confirm version-specific behaviour with the PHP project and test it against the project’s actual database and runtime.

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