Filter array based on the second array

I have an array $dataand need to filter it based on another array $clr. I did this with help foreachand decided my goal, but I am looking for the best way, for example mapor filter. I tried:

$clr = [1, 2, 4, 6, 8, 13, 21];
$data = [2, 3, 8];

foreach($clr as $val)
{
    if(($key = array_search($val, $data)) !== false) unset($data[$key]);
}

print '<pre>';
print_r($data);

Any of your suggestions will be appreciated.

+4
source share
1 answer

You can use the array_diff($data, $clr); live demo .

+2
source

Source: https://habr.com/ru/post/1676642/


All Articles