note 96818 added to function.explode
| From: | gareth dot williams at antaisx dot com | Date: | Wed, 17 Mar 2010 12:48:32 +0000 |
| Subject: | note 96818 added to function.explode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-167133@lists.php.net to get a copy of this message | ||
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!
----
Server IP: 222.73.207.145
Probable Submitter: 127.0.0.1 (proxied: 125.107.91.4)
----
Manual Page -- http://www.php.net/manual/en/function.explode.php
Edit -- https://master.php.net/note/edit/96818
Del: integrated -- https://master.php.net/note/delete/96818/integrated
Del: useless -- https://master.php.net/note/delete/96818/useless
Del: bad code -- https://master.php.net/note/delete/96818/bad+code
Del: spam -- https://master.php.net/note/delete/96818/spam
Del: non-english -- https://master.php.net/note/delete/96818/non-english
Del: in docs -- https://master.php.net/note/delete/96818/in+docs
Del: other reasons-- https://master.php.net/note/delete/96818
Reject -- https://master.php.net/note/reject/96818
Search -- https://master.php.net/manage/user-notes.php