note 95926 added to function.array-search
| From: | jamesatbandit dot co dot nz at osu1 dot php dot net | Date: | Fri, 29 Jan 2010 01:33:21 +0000 |
| Subject: | note 95926 added to function.array-search | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-165476@lists.php.net to get a copy of this message | ||
A couple of staple custom PHP array searching functions I rely on in most of my projects;
<?php
// array_search with partial matches and optional search by key
function array_find($needle, $haystack, $search_keys = false) {
if(!is_array($haystack)) return false;
foreach($haystack as $key=>$value) {
$what = ($search_keys) ? $key : $value;
if(strpos($what, $needle)!==false) return $key;
}
return false;
}
// array_search with recursive searching, optional partial matches and optional search by key
function array_find_r($needle, $haystack, $partial_matches = false, $search_keys = false) {
if(!is_array($haystack)) return false;
foreach($haystack as $key=>$value) {
$what = ($search_keys) ? $key : $value;
if($needle===$what) return $key;
else if($partial_matches && @strpos($what, $needle)!==false) return $key;
else if(is_array($value) && array_find_r($needle, $value, $partial_matches,
$search_keys)!==false) return $key;
}
return false;
}
?>
Hope they help someone!
----
Server IP: 69.147.83.197
Probable Submitter: 121.98.81.27
----
Manual Page -- http://www.php.net/manual/en/function.array-search.php
Edit -- https://master.php.net/note/edit/95926
Del: integrated -- https://master.php.net/note/delete/95926/integrated
Del: useless -- https://master.php.net/note/delete/95926/useless
Del: bad code -- https://master.php.net/note/delete/95926/bad+code
Del: spam -- https://master.php.net/note/delete/95926/spam
Del: non-english -- https://master.php.net/note/delete/95926/non-english
Del: in docs -- https://master.php.net/note/delete/95926/in+docs
Del: other reasons-- https://master.php.net/note/delete/95926
Reject -- https://master.php.net/note/reject/95926
Search -- https://master.php.net/manage/user-notes.php