PHP Compare Two Array for Matches

In this tutorial, you will learn how to compare two or more arrays in PHP and create new arrays with match values.

How to Compare Two or More Array for Matches in PHP?

You can use array_intersect() function of PHP for that:

The PHP array_intersect() function compares the values of two or more array values, and it returns a new array with the match values.

Here is syntax of array_intersect():

The syntax of array_intersect() function is following:

 array_intersect(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 – PHP Compare two arrays For a match

Let’s take the first example:

$array1 = array('apple', 'banana', 'orange', 'pear');
$array2 = array('banana', 'pear', 'grape', 'watermelon');
$common = array_intersect($array1, $array2);
print_r($common);

Example 2 – Compare Numeric Array in PHP

Let’s take the second example, in this example, you have two numeric arrays with match values:

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

The result of the above code is: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )

Example 3 – Compare three arrays for matches

Let’s take the third example for compare three array:

$array1 = array('apple', 'banana', 'orange', 'pear');
$array2 = array('banana', 'pear', 'grape', 'watermelon');
$array3 = array('pear', 'pineapple', 'kiwi', 'orange');
$common = array_intersect($array1, $array2, $array3);
print_r($common);

Conclusion

In conclusion, the array_intersect() function is a useful tool for comparing arrays for matches in PHP.

Recommended Posts:

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

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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