Jump to content

deep_array_search function


knowram

Recommended Posts

I found a deep_in_array function on php.net which I modified to be a deep_array_search function. But for some reason it only works if the key is not 0.

 

example

 

function deep_array_search($value, $array, $case_insensitive = false){
    foreach($array as $item){
        if(is_array($item)) $ret = array_search($value, $item, $case_insensitive);
        else $ret = ($case_insensitive) ? strtolower($item)==$value : $item==$value;
        if($ret)  return $ret;
    }
    return false;
}

$a = array('1' => array('0' => 's'), '2' => array('0' => 'p', '1' => 'r'), '3' => array('0' => 'o'));
$b = array('1' => array('0' => 's'), '2' => array('1' => 'p', '2' => 'r'), '3' => array('0' => 'o'));

$ret = deep_array_search('p', $a);

    echo ($ret); // $ret seems to be nothing 

$ret = deep_array_search('p', $b);

    echo ($ret); // $ret returns "1" like it should

 

Any ideas why this is happening?

 

Thanks for the help.

Link to comment
https://forums.phpfreaks.com/topic/52685-deep_array_search-function/
Share on other sites

function deep_array_search($value, $array, $case_insensitive = false){
foreach($array as $item){
  if(is_array($item)) $ret=deep_array_search($value, $item, $case_insensitive);
  else $ret = ($case_insensitive) ? strtolower($item)==$value : $item==$value;
  if($ret===true)  return $ret;
}
return false;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.