Page 1 of 1

[SOLVED] Drop empty keys (arrays)

Posted: Fri Jun 29, 2007 9:48 pm
by mevets
I have an array of users. I have another array that contains users from the first array that I want deleted. This how I've attempted it:

Code: Select all

$masterlist = array(0 => "user0", 1 => "user1", 2 => "user2", 3 => "user3");
$dellist = array(0 => "user1", 1 => "user2");
foreach ($dellist as $needle) {
  if (array_search($needle, $masterlist));
    $masterlist[array_search($needle, $masterlist)] = null; 
}
print_r($masterlist);
I'm left with an array with holes in it. Is there a way to now collapse the array where there are null values? Should I be taking another approach all together?

Posted: Fri Jun 29, 2007 9:49 pm
by feyd
array_filter() is the answer to your specific question, but array_diff() may be of interest.

Posted: Fri Jun 29, 2007 11:16 pm
by mevets
thanks, solution

Code: Select all

$masterlist = array(0 => "user0", 1 => "user1", 2 => "user2", 3 => "user3");
$dellist = array(0 => "user1", 1 => "user2");
$diff = array_diff($masterlist, $dellist);
print_r($diff);