0

this is a very similar question to Best solution to remove duplicate values from case-insensitive array

In the accepted answer the 1st comment mentions using trim() to remove whitespace as well.

I'd like to do this but I can't figure out where the trim should go

can anyone help me?

$r = array_intersect_key($input, array_unique(array_map('strtolower', $input)));
Community
  • 1
  • 1
crazy sarah
  • 611
  • 4
  • 13
  • 29

2 Answers2

0

just like this:

$input = array('test ', 'test', ' test2', 'test2');
$r = array_intersect_key($input, array_unique(array_map('strtolower', array_map('trim',$input))));

Fiddle

If you need to trim the results also, you should apply array map on the results too

$r = array_map('trim',array_intersect_key($input, array_unique(array_map('strtolower', array_map('trim',$input)))));
mamdouh alramadan
  • 8,349
  • 6
  • 36
  • 53
0

You can use some method like array_walk in order to trim each item of the array before delete duplicates, like:

function trimming(&$val, $key) {
    $val = trim ($val);
}

$data = array("  vvv  ","ccc   ");
array_walk($data, 'trimming');
print_r($data);

And then you can use your code to delete duplicates.

ssola
  • 368
  • 1
  • 9