note 64598 added to function.array-search
| From: | gullevek at gullevek dot org | Date: | Wed, 19 Apr 2006 05:31:32 +0000 |
| Subject: | note 64598 added to function.array-search | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-108493@lists.php.net to get a copy of this message | ||
There were two previous entries for having a recursive search. The first one only searched for
values, second one for values with an optional key.
But both of those stopped after they found an entry. I needed, that it searches recursive, with
optional key and returns me all matches found in the array.
So I wrote this function:
needle is the value you search, haystack is the array of course, key is the optional key in the
array where the needle should be. path should be never set on intial call. its an internal used
variable.
It returns an array $path with the array entry 'found' where you can find all found
groups. In these groups you have the array which holds the keys to find the data.
I hope this helps some of you.
<?php
function array_search_recursive_all($needle, $haystack, $key, $path = NULL)
{
if (!$path['level'])
$path['level'] = 0;
if (!$path['work'])
$path['work'] = array();
if (!is_array($haystack))
$haystack = array();
// go through the array,
foreach ($haystack as $_key => $_value)
{
// only value matches
if (is_scalar($_value) && $_value == $needle && !$key)
{
$path['work'][$path['level']] = $_key;
$path['found'][] = $path['work'];
}
// key and value matches
elseif (is_scalar($_value) && $_value == $needle && $_key == $key)
{
$path['work'][$path['level']] = $_key;
$path['found'][] = $path['work'];
}
elseif (is_array($_value))
{
// add position to working
$path['work'][$path['level']] = $_key;
// we will up a level
$path['level'] += 1;
// call recursive
$path = array_search_recursive_all($needle, $_value, $key, $path);
}
}
// cut all that is >= level
array_splice($path['work'], $path['level']);
// step back a level
$path['level'] -= 1;
return $path;
}
?>
If you call it with this:
<?
$right_side = array ('foo' => 'alpha', 'bar' =>
'beta', 'delta' => 'gamma', 'gamma' =>
'delta');
$value = 'beta';
$key = 'bar';
$pos = array_search_recursive_all($value, $right_side, $key);
?>
You will find in $pos this data
<?
Array
(
[level] => -1
[work] => Array
(
)
[found] => Array
(
[0] => Array
(
[0] => bar
)
)
)
?>
----
Server IP: 66.163.161.117
Probable Submitter: 211.14.136.222
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.array-search.php
Edit -- http://master.php.net/note/edit/64598
Del: integrated -- http://master.php.net/note/delete/64598/integrated
Del: useless -- http://master.php.net/note/delete/64598/useless
Del: bad code -- http://master.php.net/note/delete/64598/bad+code
Del: spam -- http://master.php.net/note/delete/64598/spam
Del: non-english -- http://master.php.net/note/delete/64598/non-english
Del: in docs -- http://master.php.net/note/delete/64598/in+docs
Del: other reasons-- http://master.php.net/note/delete/64598
Reject -- http://master.php.net/note/reject/64598
Search -- http://master.php.net/manage/user-notes.php