note 80902 deleted from function.array-search by levim
| From: | levim@php.net | Date: | Mon, 24 Feb 2014 22:42:07 +0000 |
| Subject: | note 80902 deleted from function.array-search by levim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-198653@lists.php.net to get a copy of this message | ||
Note Submitter: mjaning at gmail dot com
----
Hi! Based on Chris function, I made another to simplify code and improve diferents features...
With this function you can:
- Filter Key and Values recursively
- Call function many times using previows result
- Final result always will be an array numeric index contenting a value or sigle array
key=>value.
Thanks for PHP!
<?php
function array_search_recursive($needle, $haystack, $nodes=array())
{
foreach ($haystack as $key1=>$value1)
{
if (is_array($value1))
$nodes = array_search_recursive($needle, $value1, $nodes);
elseif (($key1 == $needle) or ($value1 == $needle))
$nodes[] = array($key1=>$value1);
}
return $nodes;
}
$arg[] = array("column1"=>"Class3");
$arg[] = array("column2"=>"Class1");
$arg[] = array("column3"=>"Class3");
$arg[] = array("column4"=>"Class4");
$arg[] = array("column4"=>"Class3");
$arg[] = "column3";
$filter = array_search_recursive("Class3",$arg);
echo "<hr>";var_dump($filter);
$filter = array_search_recursive("column3",$filter);
echo "<hr>";var_dump($filter);
?>