note 101147 deleted from functions.user-defined by danbrown
| From: | danbrown@php.net | Date: | Mon, 29 Nov 2010 19:48:31 +0000 |
| Subject: | note 101147 deleted from functions.user-defined by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-174453@lists.php.net to get a copy of this message | ||
Note Submitter: amiller at connext dot net
----
function useful_recursion($haystack, $needle) {
# Recursive implementation of in_array.
# Not recommended for production use; PHP's stack is
# too short. Use builtin in_array() instead.
if (!is_array($haystack)) {
trigger_error("First argument to useful_recursion() must be an array.", E_USER_ERROR);
};
if (count($haystack) == 0) {
# nothing left in the haystack, and we didn't find the needle
return false;
};
# take the next element of $haystack for comparison
# (note that this shortens $haystack by one each time
# through the function)
$straw = array_pop($haystack);
if ($needle == $straw) {
# found it!
return true;
};
# this straw isn't the needle, but there's more hay in the
# stack -- here's where we recur
return useful_recursion($haystack, $needle);
}