Share on Google+Share on Google+

php array difference / php array diff

Advertisement
This section demonstrates how to compute the differences between given arrays

  • PHP array_diff() function is used to return the difference elements present in the given arrays.
  • arrays can be two or more.


PHP Array Diff() Function Example

<?php

    $ar1=array(11,12,13,14,15,16,17,18,19,20);
    $ar2
=array(10,12,13,14,15,16,17,18,19,21);
    $ar3
=array_diff($ar1,$ar2);

    echo "array1 <br>";

    foreach ($ar1 as $a1){
      echo
" ".$a1;
     }

    echo "<br>array2 <br>";

    foreach ($ar2 as $a2){
    echo
" ".$a2;
     }

    echo "<br>difference is ".$dif;

    foreach ($ar3 as $a3){
    echo
" ".$a3;
    }
?>

Output
    array1
    11 12 13 14 15 16 17 18 19 20
    array2
    10 12 13 14 15 16 17 18 19 21
    difference is 11 20

 

Advertisements

Advertisement

Posted on: October 28, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.