note 64598 modified in function.array-search by danbrown
| From: | danbrown@php.net | Date: | Wed, 29 Apr 2009 13:01:23 +0000 |
| Subject: | note 64598 modified in function.array-search by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-153985@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:
<?php
$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
)
)
)
--was--
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
)
)
)
?>
http://php.net/manual/en/function.array-search.php