5/14/2012

PHP: Custom Array Filter

$my_array = array(2, "a", null, 2.5, NULL, 0, "", 8, -1,array());
 
class ArrayFilter
{
 public static function not_empty($v) 
 {
   if($v !== null && trim($v) !== '' && $v !== array())
   {
  return true;
   }
 }
 
 function filter($values)
 {
  return array_filter($values, "self::not_empty");
 }
}
$arrayFilter = new ArrayFilter();
print_r($arrayFilter->filter($my_array));
Result:
C:\>php -f filter.php
Array
(
    [0] => 2
    [1] => a
    [3] => 2.5
    [5] => 0
    [7] => 8
    [8] => -1
)
C:\>

No comments:

Post a Comment

The 0/1 Knapsack Problem - Dynamic Programming

 References: https://www.youtube.com/watch?v=EH6h7WA7sDw  Class Item class Item { private $_weight; private $_value; public functi...