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. Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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 Quote Link to comment 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; } Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.