Home Tutorial Php Phparray php array difference / php array diff

 
 

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

 

Related Tags for php array difference / php array diff :


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.