Articles

How to Compare Two or More Array Values in PHP

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

How to Compare Two or More Array Values in PHP

Array_diff() function compares the values of two or more arrays values in PHP. And returns a new array with unique values.

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_secondThis 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:

    

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:

    

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:

    

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

Conclusion

Compare arrays in PHP. In this tutorial, we have learned how to compare two or more arrays in PHP and create a unique array without duplicate values.

Recommended PHP Tutorials

  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

Images mentioned above related to PHP are either copyright property of respective image owners.

Rabins Sharma Lamichhane

Rabins Sharma Lamichhane is the owner of RabinsXP who is constantly working for increasing the Internet of Things (IoT) in Nepal. He also builds android apps and crafts beautiful websites. He is also working with various social services. The main aim of Lamichhane is to digitally empower the citizens of Nepal and make the world spiritually sound better both in terms of technology and personal development. Rabins is also the first initiator of Digital Nepal.

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button