knowram Posted May 23, 2007 Share Posted May 23, 2007 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 More sharing options...
taith Posted May 23, 2007 Share Posted May 23, 2007 because regardless of wether $item is an array or not, it is beign given a value... therefore, regardless of anything... $ret is always being return;ed therefore, function is over. Link to comment https://forums.phpfreaks.com/topic/52685-deep_array_search-function/#findComment-260102 Share on other sites More sharing options...
knowram Posted May 23, 2007 Author Share Posted May 23, 2007 I am not quite sure I fallow. What do I need to do to fix this problem? Link to comment https://forums.phpfreaks.com/topic/52685-deep_array_search-function/#findComment-260112 Share on other sites More sharing options...
knowram Posted May 23, 2007 Author Share Posted May 23, 2007 Or if there is a way to start an array with the key of 1 instead of 0 that would work to Link to comment https://forums.phpfreaks.com/topic/52685-deep_array_search-function/#findComment-260120 Share on other sites More sharing options...
taith Posted May 23, 2007 Share Posted May 23, 2007 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; } Link to comment https://forums.phpfreaks.com/topic/52685-deep_array_search-function/#findComment-260124 Share on other sites More sharing options...
knowram Posted May 23, 2007 Author Share Posted May 23, 2007 All good figured that one out. Thanks for the help Link to comment https://forums.phpfreaks.com/topic/52685-deep_array_search-function/#findComment-260138 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.