note 101147 added to functions.user-defined
| From: | amiller at connext dot net | Date: | Mon, 29 Nov 2010 19:44:22 +0000 |
| Subject: | note 101147 added to functions.user-defined | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-174452@lists.php.net to get a copy of this message | ||
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);
}
----
Server IP: 208.69.120.55
Probable Submitter: 209.116.252.179
----
Manual Page -- http://www.php.net/manual/en/functions.user-defined.php
Edit -- https://master.php.net/note/edit/101147
Del: integrated -- https://master.php.net/note/delete/101147/integrated
Del: useless -- https://master.php.net/note/delete/101147/useless
Del: bad code -- https://master.php.net/note/delete/101147/bad+code
Del: spam -- https://master.php.net/note/delete/101147/spam
Del: non-english -- https://master.php.net/note/delete/101147/non-english
Del: in docs -- https://master.php.net/note/delete/101147/in+docs
Del: other reasons-- https://master.php.net/note/delete/101147
Reject -- https://master.php.net/note/reject/101147
Search -- https://master.php.net/manage/user-notes.php