Home Tutorial Php Phparray php array difference / php array diff

 
 

Share on Google+Share on Google+
php array difference / php array diff
Posted on: October 28, 2009 at 12:00 AM
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

 

Advertisement

Related Tags for php array difference / php array diff :


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.

Posted on: October 28, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com