note 110497 deleted from function.array-diff by tularis

From: Date: Tue, 30 Oct 2012 00:11:34 +0000
Subject: note 110497 deleted from function.array-diff by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-191590@lists.php.net to get a copy of this message
Note Submitter: iraitc at gmail dot com ---- <? function array_filter_recursive(array $input, $fn = null) { $defaultfn = function($val) { return (!empty($val)); }; if (!$fn) $fn = $defaultfn; $result = array(); foreach ($input as $key => $val) { if (is_array($val)) { $recur = array_filter_recursive($val, $fn); if ($recur !== null) $result[$key] = $recur; } else if ($fn) { if ($fn($val)) { $result[$key] = $val; } } } if (!empty($result)) return $result; } // ANONYMOUS CALLBACK FUNCTION $fn = function ($value) { if ($value === 0 || strpos($value,'empty') || $value===null) return false; else return true; }; // PRE FILTER TEST DATA $preFilter = array( 'name' => 'hamed', 'family'=>'pk', 'postal_code'=>7185633282, 'point'=>0, 'blah'=>null, 'b' => array( 'drop' => 'this is empty!', 'keep' => 'keep me!', ), ); // EXAMPLE USAGE CODE echo '<pre>'; echo '// print_r($preFilter);' . "\n"; print_r($preFilter); $postFilter = array_filter_recursive($preFilter, $fn); echo "\n"; echo '// print_r($postFilter);' . "\n"; print_r($postFilter); /* *************** OUTPUT ********************** // print_r($preFilter); Array ( [name] => hamed [family] => pk [postal_code] => 7185633282 [point] => 0 [blah] => [b] => Array ( [drop] => this is empty! [keep] => keep me! ) ) // print_r($postFilter); Array ( [name] => hamed [family] => pk [postal_code] => 7185633282 [b] => Array ( [keep] => keep me! ) ) */

« previous php.notes (#191590) next »