In PHP, you can easily compare arrays using the array_intersect()
function, which returns an array containing all the values that are present in all the input arrays. In this tutorial, you will learn how to compare two or more arrays in PHP and create new arrays with match values.
This tutorial has the purpose to explain to you compare two or more array in PHP with examples.
PHP Compare Arrays for Matches
Before you compare arrays in PHP for the match value, You should know about the array_intersect() function of PHP. Let’s see the definition, syntax, and examples of array_intersect() function of PHP
PHP array_intersect() Function
Definition:– The PHP array_intersect() function compares the values of two or more arrays values. And returns a new array with the match values.
Syntax
The syntax of array_intersect() function is following:
array_intersect(array_one, array_second, array_third, …, array_n);
Parameters of array_diff() function:
Parameter | Description |
---|---|
array_one | This is required. The array to compare from. |
array_second | This is required. An array to compare against. |
Example 1 – PHP Compare two arrays For match
The array_intersect()
function takes two or more arrays as arguments and returns an array containing the values that are present in all the input arrays. For example, consider the following code:
$array1 = array('apple', 'banana', 'orange', 'pear'); $array2 = array('banana', 'pear', 'grape', 'watermelon'); $common = array_intersect($array1, $array2); print_r($common);
This code creates two arrays, $array1
and $array2
, containing a few fruits. It then calls the array_intersect()
function with both arrays as arguments, and stores the result in the $common
variable. Finally, it prints the contents of the $common
array using the print_r()
function.
When you run this code, you will see that the $common
array contains the elements 'banana'
and 'pear'
, which are the fruits that appear in both $array1
and $array2
.
Example 2 – Compare Numeric Array in PHP
Let’s take the second example, in this example, you have two numeric arrays with match values. Using the array_intersect() function, you will get match values from these arrays and return a new array in PHP:
<?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 – PHP compare three arrays for matches
You can use the array_intersect()
function with any number of input arrays, as long as they are separated by commas. For example, consider the following code:
$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);
This code creates three arrays, $array1
, $array2
, and $array3
, containing fruits. It then calls the array_intersect()
function with all three arrays as arguments, and stores the result in the $common
variable. Finally, it prints the contents of the $common
array using the print_r()
function.
When you run this code, you will see that the $common
array contains only the element 'pear'
, which is the fruit that appears in all three input arrays.
In addition to comparing arrays for matches, you can also use the array_intersect()
function to compare objects, as long as the objects have the same properties and values. The function will return an array of objects that have the same properties and values as the objects in the input arrays.
Conclusion
In conclusion, the array_intersect()
function is a useful tool for comparing arrays for matches in PHP. It allows you to easily find the common elements in two or more arrays, which can be helpful in a wide variety of programming tasks.
Recommended Posts:
- Compare Arrays PHP | PHP array_diff() Function
- Get, Write, Read, Load, JSON File from Url PHP
- Functions: Remove First Character From String PHP
- Remove Specific/Special Characters From String In PHP
- How to Replace First and Last Character From String PHP
- Reverse String in PHP
- Array Push, POP PHP | PHP Array Tutorial
- PHP Search Multidimensional Array By key, value and return key
- json_encode()- Convert Array To JSON | Object To JSON PHP
- PHP remove duplicates from multidimensional array
- PHP Remove Duplicate Elements or Values from Array PHP