Compare Arrays PHP | PHP array_diff() Function

Compare Arrays PHP | PHP array_diff() Function

Compare arrays in PHP. In this tutorial, you will learn how to compare two or more arrays in PHP and create a unique array without duplicate values.

This tutorial has the purpose to explain to you the compare two or more array in PHP with examples’

Compare Array PHP

Before we compare arrays in PHP, You should know about the array_diff() function of PHP. Let’s see the definition, syntax, and examples of array diff() function of PHP

PHP array_diff() Function

Definition:– The PHP array_diff() function compares the values of two or more arrays values. And returns a new array with unique values.

Syntax

The syntax of array_diff() function is following:

array_diff(array_one, array_second, array_third, …, array_n);

Parameters of array_diff() function:

ParameterDescription
array_oneThis is required. The array to compare from.
array_second This is required. An array to compare against.

Example 1 – Compare two arrays in PHP

Let’s take first example, in this example, we have two arrays with duplicate values. And we will remove the duplicate values from these arrays and create a new unique array in PHP:

    <?php
    $array_one = array("first", "second", "third", "fourth", "five");
    $array_second = array("first", "second", "third", "fourth");

    $res = array_diff($array_one,$array_second);

    print_r($res);
    ?>

The result of the above code is: Array ( [4] => five )

Example 2 – Compare Numeric Array in PHP

Let’s take the second example, in this example, we have two numeric arrays with duplicate values. And we will remove the duplicate values from these arrays and create a new unique array in PHP:

    <?php
    $array_one = array(1, 2, 3, 4, 5, 6);
    $array_second = array(1, 2, 3, 4);

    $res = array_diff($array_one,$array_second);

    print_r($res);
    ?>

The result of the above code is: Array ( [4] => 5 [5] => 6 )

Example 3 – Compare Three Arrays in PHP

Let’s take the Third example, in this example, we have three arrays with duplicate values. And we will compare these three arrays and find the difference between PHP:

    <?php

    $array1 = array('a', 'b', 'c', 'd', 'e', 'f'); 

    $array2 = array('a', 'b', 'g', 'h'); 
    
    $array3 = array('a', 'f', 'i'); 

    $res = array_diff($array1, $array2, $array3);

    print_r($res);
    ?>

The output of the above code is: Array ( [2] => c [3] => d [4] => e )

You may like

Recommended Posts:

  1. Get, Write, Read, Load, JSON File from Url PHP
  2. Functions: Remove First Character From String PHP
  3. Remove Specific/Special Characters From String In PHP
  4. How to Replace First and Last Character From String PHP
  5. Reverse String in PHP
  6. Array Push, POP PHP | PHP Array Tutorial
  7. PHP Search Multidimensional Array By key, value and return key
  8. json_encode()- Convert Array To JSON | Object To JSON PHP
  9. PHP remove duplicates from multidimensional array
  10. PHP Remove Duplicate Elements or Values from Array PHP

AuthorTuts Make

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Javascript, JQuery, Laravel, Codeigniter, Vue JS, Angular JS, React Js, WordPress and Bootstrap from a starting stage.

Leave a Reply

Your email address will not be published. Required fields are marked *