Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jeevamugunthan's avatar

array search with array()

hello everyone ,

i have a 2 array values like

$worker=Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )

$other=Array ( [0] => 1 [1] => 2 [2] => 4  )

in this i need to take count of matching elements, and then take not matching elements of workers in to another array .

anyone please give the solution of this, thank you in advance..

0 likes
4 replies
abhijeet9920's avatar
Level 5

Hello @jeevamugunthan ,

Based on your request, I think below solution will help you. With

<?php
    $worker=array(1,2,3,4);
    $other=array(1,2,4);
    $found = [];
    $not_found = [];
    foreach($worker as $item){
        if(in_array($item, $other))
            $found[] = $item;
        else
            $not_found[] = $item;
    }
    return ['found' => $found, 'not_found' => $not_found];
?>

With using array functions


<?php
    $diff_result = array_diff($worker, $other);
    return $diff_result;
?>

In both examples, we are searching every element from $worker array in $other array.

You can visit php array_diff.

Hope this helps you.

1 like
UsmanBasharmal's avatar

Try this

 $worker = collect([1, 2, 3, 4]); 
 $matchingelements = $worker->concat([1, 2, 4]);


 return $matchingelements->duplicates()->count(); // this will give you count of matching elements 

//This will give you not matching values

 $worker = collect([1, 2, 3, 4]);

 $diff = $worker->diff([1, 2, 4]);


 return $diff->count();

Please or to participate in this conversation.