Getting the Similar and Difference Between Arrays in PHP Tutorial

This tutorial tackles getting the Similar and Difference Between Arrays in PHP Language. The purpose of this article is to give an idea to students, self-learners, or those who are new to PHP Language on how they can capture or get the different or similar values or keys between 2 arrays. Here, other PHP built-in functions for arrays are also explained.

What is Array?

An Array is a group of items or information kept in contiguous memory locations. By using the index or key, we can easily get the data value directly. In PHP, we can simply create an array using the PHP array() function or using Brackets ( [] ).

Syntax

Here's a sample snippet for creating an array in PHP.

  1. <?php
  2. $array = array("apple", "banana", "orange");
  3. //output Array(
  4. // 0 => 'apple',
  5. // 1 => 'banana',
  6. // 2 => 'orange',
  7. //)
  8. $array2 = ["PHP", "Python", "C#"];
  9. //output Array(
  10. // 0 => 'PHP',
  11. // 1 => 'Python',
  12. // 2 => 'C#',
  13. //)
  14. $array3 = ["a"=>"Mark", "b"=>"John", "c"=>"Mike"]; \\ or array("a"=>"Mark", "b"=>"John", "c"=>"Mike")
  15. //output Array(
  16. // a => 'Mark',
  17. // b => 'John',
  18. // c => 'Mike',
  19. //)
  20. ?>

How can you get Similar and Difference Values Between 2 Arrays in PHP?

Similar and Difference Values Between 2 Arrays in PHP

Getting the Similar Values Between 2 Arrays in PHP?

PHP comes with a built-in function called array_intersect(). This function allows you to compute the intersections of an array. The function returns an array of values that exists in both given arrays.

Syntax

You can simply use the following syntax to use the array_intersect() function.

  1. <?php
  2. array_intersect((array) $array, (array) $arrays);
  3. ?>

The first parameter of the array_intersect() function is the array with values to check and the second parameter is the array to compare.

Example

  1. <?php
  2. $array1 = ["Nike", "Vans", "Converse"];
  3. $array2 = ["Vans", "Fila"];
  4. $similar = array_intersect($array1, $array2);
  5. print_r($similar);
  6. ?>

Result

    Array ( [ 1 ] => Vans )

For getting similar keys between two arrays, you can use the array_intersect_key() function. It computes the intersections of array keys.

Example

  1. <?php
  2. $array1 = ["a"=>"Nike", "b"=>"Vans", "c"=>"Converse"];
  3. $array2 = ["c"=>"Vans", "d"=>"Fila"];
  4. $similar = array_intersect_key($array1, $array2);
  5. print_r($similar);
  6. ?>

Result

    Array ( [ c ] => Converse )

Getting the Difference Values Between 2 Arrays in PHP?

PHP also has a built-in function for getting the different values between the 2 arrays called array_diff(). It computes the difference between arrays which is the complete opposite of the array_intersect() function.

Syntax

You can simply use the following syntax to use the array_diff() function.

  1. <?php
  2. array_diff((array) $array, (array) $arrays);
  3. ?>

The first parameter of the array_diff() function is the array with values to check and the second parameter is the array to compare.

Example

  1. <?php
  2. $array1 = ["Nike", "Vans", "Converse"];
  3. $array2 = ["Vans", "Fila"];
  4. $similar = array_diff($array1, $array2);
  5. print_r($similar);
  6. ?>

Result

    Array ( [ 0 ] => Nike [ 2 ] => Converse )

For getting difference keys between two arrays, you can use the array_diff_key() function. It computes the intersections of array keys.

Example

  1. <?php
  2. $array1 = ["a"=>"Nike", "b"=>"Vans", "c"=>"Converse"];
  3. $array2 = ["c"=>"Vans", "d"=>"Fila"];
  4. $similar = array_diff_key($array1, $array2);
  5. print_r($similar);
  6. ?>

Result

    Array ( [ a ] => Nike [ b ] => Vans )

I have also provided the source code zip file I created that demonstrates the usage of the array_intersect() and array_diff() functions of PHP on this site. Feel free to download and test it.

DEMO VIDEO

Bonus

In addition to this tutorial content, PHP also comes with built-in functions for arrays called in_array() and array_unique functions. The in_array() function is used for checking the value if it exists in an array and the array_unique function is used to remove duplicate values from the array.

Example

  1. $unique_values = array_unique(["PHP","Python","VB", "Python"]);
  2. print_r($unique_values);
  3. // output
  4. //Array ( [0] => PHP [1] => Python [2] => VB )
  5.  
  6. if(in_array("PHP", ["PHP","Python","VB"])){
  7. echo "Value exists in the array.";
  8. }else{
  9. echo "Value does not exists in the array.";
  10. }
  11. // output
  12. // Value exists in the array.

That's the end of this tutorial. I hope this Getting the Similar and Difference Between Arrays in PHP Tutorial will help you with what you are looking for and will be useful for your current and future PHP Projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding =)

Add new comment