note 44894 deleted from function.array-search by levim
| From: | levim@php.net | Date: | Mon, 24 Feb 2014 22:47:56 +0000 |
| Subject: | note 44894 deleted from function.array-search by levim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-198668@lists.php.net to get a copy of this message | ||
Note Submitter: Wouter van Vliet <me at woutervanvliet dot nl>
----
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]