note 96818 deleted from function.explode by thiago
| From: | thiago@php.net | Date: | Thu, 18 Mar 2010 02:23:21 +0000 |
| Subject: | note 96818 deleted from function.explode by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-167149@lists.php.net to get a copy of this message | ||
Note Submitter: gareth dot williams at antaisx dot com
----
If you want the list items in the string to be array keys (rather than values assigned to numeric
keys), consider using strtok instead:
function explodetokeys($sDelim, $sInput) {
$aResult=Array();
$sArrayKey=strtok($sInput, $sDelim);
while ($sArrayKey !== false) {
$aResult[$sArrayKey]=1;
$sArrayKey=strtok($sDelim);
}
}
explodetokeys(' ', 'one two three'); will return:
Array('one'=>1, 'two'=>1, 'three'=>1)
This is useful if you need to search for the existence of a string in the string many times, as
searching an ordered array using isset($array[$string]) in a loop should be much faster than using
strpos many times in a loop.
If you know of a better way of doing this, please reply!