What have you tried? My first go would be using array_diff()
Apr 19, 2020
4
Level 2
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..
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
Please or to participate in this conversation.