Search code examples
phparraysarray-difference

difference between two array using php


I am Having Two array like the following ,

$a =array(1,2,3,4);
$b =array(1,2,5,6);

here is mycode

$c=array_diff($a,$b);
$c=array(3,4,5,6);

but i want it like the following

$c=array(3,4);

Note:

i want $a array value which is not present in $b array.


Solution

  • try this

    <?php
    $array1 = array(1,2,3,4);
    $array2 = array(1,2,5,6);
    $result = array_diff($array1, $array2);
    
    print_r($result);
    ?>
    

    read this PHp.NET for array_diff and also refer this FIDDLE