3

I have an array $data and it is required to be filtered based on another array $clr. I have done it by foreach and solved my purpose but I am looking for an optimum way like map or filter. What I have tried is:

$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 suggestion will be appreciated.

LF00
  • 27,015
  • 29
  • 156
  • 295
Janie
  • 638
  • 9
  • 26

1 Answers1

2

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

LF00
  • 27,015
  • 29
  • 156
  • 295