note 44894 added to function.array-search
| From: | WoutervanVliet | Date: | Thu, 19 Aug 2004 15:55:09 +0000 |
| Subject: | note 44894 added to function.array-search | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-74934@lists.php.net to get a copy of this message | ||
I was looking for a way to use a user defined function for array_search and eventually came up
writing my own. Which was remarkably simple :P. Let me share this:
<?php
function array_usearch($cb, $ndl, $hs, $strict=false) {
if (!is_array($hs)) user_error('Third argument to array_usearch is expected to be an array,
'.gettype($hs).' given', E_USER_ERROR);
foreach($hs as $key=>$value) if (call_user_func_array($cb, Array($ndl, $value, $key, $strict)))
return $key;
};
?>
I'm not sure if I'm following correct conventions to specify the callback as the first
argument, but it seemed most logical to me to not interrupt the order of the other four arguments
(mixed needle, array haystack, boole strict).
[so far for my first post to the php notes]
----
Manual Page -- http://www.php.net/manual/en/function.array-search.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+44894
Delete -- http://master.php.net/manage/user-notes.php?action=delete+44894&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+44894&report=yes
Search -- http://master.php.net/manage/user-notes.php