Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 3rd, 2012  |  syntax: None  |  size: 1.01 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3. // found at http://php.net/manual/en/function.array-filter.php
  4.  
  5. function array_reindex(array $source, $blacklist = array())
  6. {
  7.     $i = 0;
  8.     foreach ($source as $key => $val) {
  9.         if ($key != $i) {
  10.             unset($source[$key]);
  11.             $source[$i] = $val;
  12.         }
  13.        
  14.         $i++;
  15.     }
  16.    
  17.     foreach ($source as $key => $val) {
  18.         foreach ($blacklist as $var) {
  19.             if ($val === $var) {
  20.                 unset($source[$key]);    
  21.                 $source = reindex($source, $blacklist);
  22.             }
  23.         }
  24.     }
  25.    
  26.     return $source;
  27. }
  28.  
  29. // Examples:
  30.  
  31. // Create a simple array
  32. $input = array(1 => 'red', 3 => 'green', 5 => 'blue', 7 => TRUE, 9 => FALSE, 11 => NULL);
  33.  
  34. // NOTE: If you have an array (look above) and your blacklist will look like this: array(TRUE, FALSE, NULL)
  35. //          Filter will delete array keys with value TRUE, FALSE or NULL
  36. $blacklist = array(TRUE, FALSE, NULL);
  37.  
  38. // Output is: Array ( [0] => red [1] => green [2] => blue )
  39. print_r( reindex($input, $blacklist) );