note 58974 added to function.array-search
| From: | markmeves at osu1 dot php dot net | Date: | Tue, 22 Nov 2005 23:13:11 +0000 |
| Subject: | note 58974 added to function.array-search | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-98943@lists.php.net to get a copy of this message | ||
A quick-and-dirty array_search_all() that i used for a small
dup-checking routine.
there are many, many ways to do something like this, not
the worst of which would be to use a relational database
for a dataset any larger than this ;)
-mark meves
<?php
/**
@return array of zero or more keys form $aHaystack whose
values match $mScalarNeedle using a
'==', (ie not strict) comparison
*/
function array_search_all($mScalarNeedle,$aHaystack){
return array_keys( array_filter($aHaystack,
create_function('$v','return $v ==
\''.addslashes($mScalarNeedle).'\';')
));
}
/*
test it:
*/
$aNicknames = array('jimmy'=>1,'james'=>1,'jim'=>1,
'billy'=>2,'william'=>2,'bill'=>2);
foreach(array('jim','bill') as $sName){
echo "variations for \"$sName\" :(".
implode(', ', array_search_all($aNicknames[$sName],$aNicknames)).
")\n";
}
/* outputs:
variations for "jim" are (jimmy, james, jim)
variations for "bill" are (billy, william, bill)
*/
?>
----
Manual Page -- http://www.php.net/manual/en/function.array-search.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+58974
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58974&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58974&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58974&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58974&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58974&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58974&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58974&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+58974&report=yes
Search -- http://master.php.net/manage/user-notes.php