note 79533 deleted from function.array-search by nlopess
| From: | nlopess@php.net | Date: | Sun, 02 Dec 2007 15:07:24 +0000 |
| Subject: | note 79533 deleted from function.array-search by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-132740@lists.php.net to get a copy of this message | ||
Note Submitter: toni dot garcia dot gutsens at gmail dot com
----
Sorry, in the previous Post, empty arrays are not unset. Add three comment code lines.
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;
}