note 96817 added to function.explode

From: Date: Wed, 17 Mar 2010 12:47:20 +0000
Subject: note 96817 added to function.explode
Groups: php.notes 
Request: Send a blank email to php-notes+get-167132@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/96817 Del: integrated -- https://master.php.net/note/delete/96817/integrated Del: useless -- https://master.php.net/note/delete/96817/useless Del: bad code -- https://master.php.net/note/delete/96817/bad+code Del: spam -- https://master.php.net/note/delete/96817/spam Del: non-english -- https://master.php.net/note/delete/96817/non-english Del: in docs -- https://master.php.net/note/delete/96817/in+docs Del: other reasons-- https://master.php.net/note/delete/96817 Reject -- https://master.php.net/note/reject/96817 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#167132) next »