PHP array_diff() function

P
PHP array_count_values() function
PHP array_diff_assoc() function

The array_diff() function compares two or more arrays and returns an array containing the difference between them. Keep in mind that this function compares the values of the array.

This function takes values from the first array given to it and compares its values with the second, third, and so on arrays. The output will be an array of differences between the first array and the rest of the others.

What is the syntax of array_diff function in PHP?

array_diff(array1, array2, array3, ...)
ParametersDetails
array1The first array to compare with other arrays – required
array2Second array to make a comparison against- required
array3, ...Further arrays to compare against – optional

Examples of array_diff function

<?php
$array_1=array("a"=>"1","b"=>"2","c"=>"3","d"=>"4");
$array_2=array("e"=>"1","f"=>"0","g"=>"0");

$difference=array_diff($array_1,$array_2);
print_r($difference);
?>
<?php
$array_1=array("a"=>"1","b"=>"2","c"=>"3","d"=>"4");
$array_2=array("e"=>"1","f"=>"0","g"=>"0");
$array_3=array("a"=>"1","b"=>"2","h"=>"9");

$difference=array_diff($array_1,$array_2,$array_3);
print_r($difference);
?>
PHP array_count_values() function
PHP array_diff_assoc() function

Add comment

Tutor Network

Share this

Learn PHP from A to Z