Duminda Chamara

Tuesday, October 27, 2009

Get diffrent elements in two arrays

How to get the diffrent elements in two arrays in PHP?


You can get the array elements which are not common to both arrays.
use the following php code.
<?php


$ar1=array("A","B","C");
$ar2=array("A","B");


$diffrence=array_diff($ar1,$ar2);


print_r($diffrence);




?>

out put:
Array ( [2] => C )
C is the item which is only common to the $ar1. Likewise array_diff returns an array of elements not common to the both  arrays.

0 comments: