How to compare two arrays in PHP?

Compare two arrays in PHP: Not only in the major programming languages but also in PHP you can compare two arrays in PHP. You can do this so by using a simple predefined function. By using those predefined functions one can merge and compare two arrays. In this article, we will get to learn how to compare two arrays in PHP.

As PHP is one of those programming languages which is developed using built-in web development capabilities, the programmers can easily embed the code to HTML code through the script tag. Though the PHP programmers need to write lengthy and complex codes, the PHP frameworks reduce time by providing code generation features. Though PHP is considered as one of the most unsecured programming languages the built-in security features and mechanisms provided by PHP frameworks make it easier for developers to protect the website. Also, the PHP developers can prevent common security threats like SQL injections, data tampering, and cross-site request forgery.

PHP Arrays

Compare two arrays in PHP

To compare two arrays in PHP, there is a built-in function associated with it. You can use this built-in function and can compare two or more than two arrays in PHP.

The function which is used to compare two or more arrays in PHP is the array_diff() function. This function compares the values of two different arrays and returns the differences. This function compares the values of two (or more) arrays, and return an array that contains the entries from array1that are not present in array2 or array3, etc.

Syntax: The syntax of the array_diff() function is as follows,

array_diff(array1, array2, array3,...);

Here are the parameters and their descriptions listed below.

array1: This array is used to compare the form
array2: This is an array which is used to compare against
array3: These following arrays are optional and they are the additional arrays to compare against.

Now, let us go through some basic examples of how you can use the compare array function in PHP to compare two or more arrays.

Example 1: Compare two arrays in PHP

Here is an example of how you can compare two arrays in PHP and return their differences.

 <?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");

$result=array_diff($a1,$a2);
print_r($result);
?>

Output: The output of the above illustrated example is as follows,

Array ( [d] => yellow )  

Example 2: Compare three arrays in PHP

Here is an example of how you can compare three arrays in PHP and return their differences.

 <?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"black","g"=>"purple");
$a3=array("a"=>"red","b"=>"black","h"=>"yellow");

$result=array_diff($a1,$a2,$a3);
print_r($result);
?>

Output: The output of the above illustrated example is as follows,

Array ( [b] => green [c] => blue )  

Intersection of Arrays

In PHP, one can intersect two arrays using the built-in Intersect function. This built-in function is used to compare the values of two or more arrays and return the matches.
The function prints only those elements of the first array that are present in all other arrays. The function which is used to intersect two arrays is the array_intersect() function.

Syntax: The syntax of the array_intersect() function is as follows,

array array_intersect($array1, $array2, $array3, $array4...) 

Parameters: The array_intersect() function takes at least two arrays as arguments. It can take any number of arrays greater than or equal to two separated by commas (‘,’).

Return type: Coming to the return type, the function returns another array containing the elements of the first array that are present in all other arrays passed as the parameter. If no element matches then, a NULL array is returned.

Example: Here is a small example on how you can perform the intersection of two or more arrays in PHP.

<?php 
// PHP function to illustrate the use of array_intersect() functionIntersect($array1, $array2, $array3)
{ $result= array_intersect($array1, $array2, $array3); return($result); }
$array1= array(5, 10, 15, 20, 25, 30); $array2= array(20, 10, 15, 55, 100, 110, 30); $array3= array(10, 15, 30, 55, 100, 95);print_r(Intersect($array1, $array2, $array3)); ?>

Output: The output of the above illustrated example is as follows,

Array 
(
[1] => 10
[2] => 15
[5] => 30
)

Merge two Arrays in PHP

You can use the array_merge() function in PHP to merge two or more functions in PHP. The merging process is done in such a way that the values of one more arrays are appended to the end of the previous array.

Syntax: For the above array_merge() function, the syntax is as follows.

array_merge(array_name1, array_name2, array_name3...)  

Parameters: Here, the array1, array2 and array3 are the names of the array. As the value to be entered is an array, the return value is also the merged array.

Example: Here is a basic example on how you can merge two arrays in PHP.

<?php
$array1 = array(1, "fruit" => "banana", 2, "monkey", 3);
$array2 = array("a", "b", "fruit" => "apple", "city" => "paris", 4, "c");

// Merging arrays together
$result = array_merge($array1, $array2);
print_r($result);
?>

If the input arrays contain the same string keys, then the later value for that key will overwrite the previous one.

FAQ’s

Here are some basic FAQ’s you can refer to clear up your doubts if any in regard to this topic. Scroll down below and explore.

Is key exist function in PHP?

The array_key_exists() function is used to check whether a specified key is present in an array or not. The function returns TRUE if the given key is set in the array. The key can be any value possible for an array index.

Is string PHP function?

The is_string() PHP function is used to check if a type of variable is a string. A string is a data type, such as floating point or integer, but it represents text rather than numbers.

What is Explode in PHP?


You can also convert between strings and arrays by using the PHP implode and explode functions: implode implodes an array to a string, and explode explodes a string into an array. To do that, you indicate the text that you want to split the string on, such as “, “, and pass that to explode.

To conclude: Hence, through this article we have got to know the different functions associated with arrays. These include merge, diff, and intersect functions. If you have any doubts or queries in regard to this article comment them below. Also express your views in the comment section below. Do share the content if you like it. For some more of interesting and exciting tech articles on web visit Morphigo.com and delve into amazing content on web.

LEAVE A REPLY

Please enter your comment!
Please enter your name here