|
Posted by Jochem Maas on 04/19/05 17:03
Richard Lynch wrote:
> On Mon, April 18, 2005 4:34 am, Sebastian said:
>
>>$string = '4:gaming,5:hardware,3:software,8:security';
>
>
> $idcats = explode(',', $string);
> while (list(, $idcat) = each($idcats)){
> list($id, $cat) = explode(':', $idcat);
> echo "\$id = $id<br />\n";
> echo "\$cat = $cat<br />\n";
> }
The 'other' guy mentioned that while() is faster than foreach,
is this true?
I read a few days ago somewhere on php.net that foreach() is the
recommended (by php devs) way of iterating over arrays....
also, compare these 2 lines:
while (list(, $idcat) = each($idcats)){ /* ... */ }
foreach ($idcats as $idcat){ /* ... */ }
now its seems to me that the foreach version is 'up' 2 function calls
on the while loop, all else being equal the foreach loop has to be faster
(given that calling functions is relatively very expensive)...
or is foreach() _really_ heavy when compared to while()?
not that I care too much, I find foreach() more pleasing to the eye and there is
less to type (in the given example).
:-)
rgds,
Jochem
>
>
>>what is the best way to explode then loop this string after its taken
>>apart.
>>
>>output should be something like:
>>
>>$id = 4
>>$cat = gaming
>>
>>etc..
>>
>>im just looking for the best/fastest way to do this. the string can grow
>>to
>>200 or so bytes, maybe more.
>
>
> 200 bytes is chump-change.
>
> It really doesn't matter how you do this, within reason.
>
[Back to original message]
|