note 79535 deleted from function.array-search by levim

From: Date: Mon, 24 Feb 2014 22:41:44 +0000
Subject: note 79535 deleted from function.array-search by levim
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-198650@lists.php.net to get a copy of this message
Note Submitter: toni dot garcia dot gutsens at gmail dot com ---- Make a recursive search on array and return the matches elements in the same structure than the original array and unset empty arrays: <?php function array_search_recursive($needle, $haystack,&$tree=Array(),$index=""){ if (is_array($haystack)){ if (count($tree)==0) $tree=array_merge(Array(),$haystack); foreach($haystack as $k=>$current){ if (is_array($current)){ array_search_recursive($needle,$current,$tree,$index."[$k]"); eval("\$a=\$tree{$index}[{$k}];"); // unset all elements = empty array if (count($a)==0) //is empty? eval("unset(\$tree{$index}[$k]);"); // unset array } else{ if ($current!=$needle){ eval("unset(\$tree{$index}[{$k}]);"); } } } } return $tree; } ?>

« previous php.notes (#198650) next »