note 32051 modified in function.array-filter by vrana

From: Date: Tue, 17 Aug 2004 13:58:03 +0000
Subject: note 32051 modified in function.array-filter by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-79974@lists.php.net to get a copy of this message
The following function modifies the supplied array recursively so that filtering is performed on multidimentional arrays as well, while preserving keys. <?php function array_cleanse(&$arr){ $temp = array(); reset($arr); if(count($arr) == 0) return ""; foreach($arr as $key=>$val): (is_array($val))? array_cleanse($val) : NULL; ($val)? $temp[$key] = $val : NULL; endforeach; $arr = $temp; reset($arr); } ?> $arr1 = array('a'=>20,'b'=>array(''),'c'=>array(1,0,2),'d'=>0); array_cleanse($arr1); $arr1 will be array('a'=>20,'c'=>array(1,2)) array_filter may not be used as it does not modify the array within itself. --was-- The following function modifies the supplied array recursively so that filtering is performed on multidimentional arrays as well, while preserving keys. function array_cleanse(&$arr){ $temp = array(); reset($arr); if(count($arr) == 0) return ""; foreach($arr as $key=>$val): (is_array($val))? array_cleanse($val) : NULL; ($val)? $temp[$key] = $val : NULL; endforeach; $arr = $temp; reset($arr); } $arr1 = array('a'=>20,'b'=>array(''),'c'=>array(1,0,2),'d'=>0); array_cleanse($arr1); $arr1 will be array('a'=>20,'c'=>array(1,2)) array_filter may not be used as it does not modify the array within itself. http://php.net/manual/en/function.array-filter.php

« previous php.notes (#79974) next »