php array difference / php array diff


 

php array difference / php array diff

This section demonstrates how to compute the differences between given arrays

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

 

Ads