php array compare code example

Example 1: php find differences between two arrays

<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);
?>

Array
(
    [1] => blue
)

Example 2: php array equality

$arraysAreEqual = ($a == $b); 	// TRUE if $a and $b have the same key/value pairs.
$arraysAreEqual = ($a === $b); 	// TRUE if $a and $b have the same key/value pairs 
								//		in the same order and of the same types.
$arraysAreEqual = (array_diff($a, $b)==[] && array_diff($a, $b) == []);

Example 3: array_diff

<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
?>

Array
(
    [1] => blue
)

Tags:

Php Example