LOKESH LOKESH - 3 months ago 14
PHP Question

Array difference for multidimensional array with single array in php

I want difference of multidimensional array with single array. I dont know whether it is possible or not. But my purpose is find diference.

My first array contain username and mobile number

array1
(
array(lokesh,9687060900),
array(mehul,9714959456),
array(atish,9913400714),
array(naitik,8735081680)
)


array2(naitik,atish
)

then I want as result

result( array(lokesh,9687060900), array(mehul,9714959456) )


I know the function array_diff($a1,$a2); but this not solve my problem. Please refer me help me to find solution.

Answer

Try this-

    $array1 = array(array('lokesh',9687060900),
    array('mehul',9714959456),
    array('atish',9913400714),
    array('naitik',8735081680));

    $array2 = ['naitik','atish'];

    $result = [];
        foreach($array1 as $val2){

        if(in_array($val2[0], $array2)){
            $result[] = $val2;
        }
    }

echo '<pre>';
print_r($result);

Hope this will help you.