PHP function of the day: array_diff()

For day 11 of 21 in the “PHP Function of the day” series, I’m going to look at array_diff(). It gets the difference between an array and one or more other arrays.

Let’s take a look.

<?php

$person1 = array(“fname”=>”Joe”,”lname”=>”Smith”);

$person2 = array(“fname”=>”Jane”,”lname”=>”Smith”);

$differences = array_diff($person1, $person2);

print_r($differences);

$person3 = array(“fname”=>”Stan”,”lname”=>”Smith”);

$differences = array_diff($person1, $person2, $person3);

print_r($differences);

?>

 

See the output

 

In the above example, $person1 is being compared to $person2 and $person3.  That’s the reason why the result in both comparisons is “Joe”.

Leave a Reply

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