|
Posted by romain.jouin on 09/20/05 22:20
Hi everyone,
I just wanted to propose this piece of code which is searching
recursively a value in an array... and let you indicate me if there is
an error...
function in_array_recursive($array, $val)
{
$r = false;
if(is_array($array))
foreach($array as $a => $b)
if($a == $val )
$r = true;
elseif( in_array_recursive($b, $val))
$r = true;
else
if($array == $val)
$r = true;
return $r;
}
I guess we could optimise it making a return each time I make a "$r ="
:
function in_array_recursive($array, $val)
{
if(is_array($array))
foreach($array as $a => $b)
if($a == $val )
return true;
elseif( in_array_recursive($b, $val))
return true;
else
if($array == $val)
return true;
return false;
}
Thanks,
JR.
Navigation:
[Reply to this message]
|