Re: [PHP] explode a string

2005-04-20 Thread Jochem Maas
Richard Lynch wrote:
On Tue, April 19, 2005 7:03 am, Jochem Maas said:
The 'other' guy mentioned that while() is faster than foreach,
is this true?

Don't know ; Don't care.
You should never loop through so many things in PHP that it matters in the
first place :-)

I read a few days ago somewhere on php.net that foreach() is the
recommended (by php devs) way of iterating over arrays

[shrug]
That's probably because they're tired of people not understanding the
internal pointer array, and asking FAQs about it.  Or maybe not.  Ask them
why they prefer it.  I sure don't know.

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

None of those are function calls.
They are all language constructs.  Okay, each() *might* be a function...
I'm not sure how much difference there is in the number of language
constructs used, nor if they are even comparable in sheer numbers the way
functions are.
ah yes, lang constructs rather than function calls.
foreach is probably slower, I think, because it creates a *copy* of the
array to work on, so it won't mess up the original and its internal
pointer.
unless I'm mistaken its a copy-on-change, so unless you are changing the
the array inside the loop you don't suffer the actuall copy penalty - can anyone
knowledgable on php internals confirm or deny this?
actually now I think of it you can use references in a foreach statement:
php -r '
$arr = array(1,2,3);
foreach($arr as $k = $v) {
$v++;
}
var_dump($arr);
'
which suggests that a copy is not (always?) being made...
Again, with 200 bytes, you are wasting your time to worry about any of this.
true, It's purely a theoretical interest - deeper understanding is alway 
nice :-)
...its not even my 200 bytes we're talking about ;-)

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()?

Why don't you just benchmark it on your own machine and find out?
because I don't have the skills to write a test/benchmark that I _know_ is
kosher (and not skewed by a million of my misconceptions, besides I run so much
stuff on my machine that speed can be severely affected by things like
apache or firebird running in the background
that and I lazy ;-) (or I just don't care enough to invest time investigating 
this)

not that I care too much, I find foreach() more pleasing to the eye and
there is
less to type (in the given example).

I'm an old dog, and I don't quite understand for sure how this new-fangled
foreach thingie works.  I'd spend more time looking it up and reading
about it than just typing what I *know* works. [shrug]



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] explode a string

2005-04-20 Thread Saswat Praharaj
explode by ,
$output1 = explode(,,$string);

use a loop and explode  array $output1 by : 

Hope this helps.

Saswat

On 4/18/05, Sebastian [EMAIL PROTECTED] wrote:
 $string = '4:gaming,5:hardware,3:software,8:security';
 
 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.
 
 should i list(), while(), explode it, or should i explode it and foreach it?
 or..?
 
 thanks.
 
 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
 


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] explode a string

2005-04-19 Thread Jochem Maas
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 = $idbr /\n;
  echo \$cat = $catbr /\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.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] explode a string

2005-04-19 Thread Petar Nedyalkov
On Tuesday 19 April 2005 17:03, Jochem Maas wrote:
 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 = $idbr /\n;
echo \$cat = $catbr /\n;
  }

 The 'other' guy mentioned that while() is faster than foreach,
 is this true?

http://www.sitepoint.com/article/php5-standard-library

Note that the crude benchmarks I've performed suggest that calling the 
methods directly is faster than using foreach, because the latter introduces 
another layer of redirection that must be resolved at runtime by PHP.


 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.

-- 

Cyberly yours,
Petar Nedyalkov
Devoted Orbitel Fan :-)

PGP ID: 7AE45436
PGP Public Key: http://bu.orbitel.bg/pgp/bu.asc
PGP Fingerprint: 7923 8D52 B145 02E8 6F63 8BDA 2D3F 7C0B 7AE4 5436


pgpFE7VCEyn7m.pgp
Description: PGP signature


Re: [PHP] explode a string

2005-04-19 Thread Jochem Maas
Petar Nedyalkov wrote:
On Tuesday 19 April 2005 17:03, Jochem Maas wrote:
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 = $idbr /\n;
 echo \$cat = $catbr /\n;
}
The 'other' guy mentioned that while() is faster than foreach,
is this true?

sorry to call you the 'other' guy, Petar - I was being lazy.
http://www.sitepoint.com/article/php5-standard-library
Note that the crude benchmarks I've performed suggest that calling the 
methods directly is faster than using foreach, because the latter introduces 
another layer of redirection that must be resolved at runtime by PHP.
are we talking about iterating over an Iterator or an array()?
Harry Fuecks is talking about iterating over a php5 object..., your
question/example features a straight array.

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.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] explode a string

2005-04-19 Thread Richard Lynch
On Tue, April 19, 2005 7:03 am, Jochem Maas said:
 The 'other' guy mentioned that while() is faster than foreach,
 is this true?

Don't know ; Don't care.

You should never loop through so many things in PHP that it matters in the
first place :-)

 I read a few days ago somewhere on php.net that foreach() is the
 recommended (by php devs) way of iterating over arrays

[shrug]

That's probably because they're tired of people not understanding the
internal pointer array, and asking FAQs about it.  Or maybe not.  Ask them
why they prefer it.  I sure don't know.

 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

None of those are function calls.

They are all language constructs.  Okay, each() *might* be a function...

I'm not sure how much difference there is in the number of language
constructs used, nor if they are even comparable in sheer numbers the way
functions are.

foreach is probably slower, I think, because it creates a *copy* of the
array to work on, so it won't mess up the original and its internal
pointer.

Again, with 200 bytes, you are wasting your time to worry about any of this.

 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()?

Why don't you just benchmark it on your own machine and find out?

 not that I care too much, I find foreach() more pleasing to the eye and
 there is
 less to type (in the given example).

I'm an old dog, and I don't quite understand for sure how this new-fangled
foreach thingie works.  I'd spend more time looking it up and reading
about it than just typing what I *know* works. [shrug]

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] explode a string

2005-04-18 Thread Petar Nedyalkov
On Monday 18 April 2005 14:34, Sebastian wrote:
 $string = '4:gaming,5:hardware,3:software,8:security';

 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.

 should i list(), while(), explode it, or should i explode it and foreach
 it? or..?

while is faster than foreach. check the iterator section in SPL for details.


 thanks.

-- 

Cyberly yours,
Petar Nedyalkov
Devoted Orbitel Fan :-)

PGP ID: 7AE45436
PGP Public Key: http://bu.orbitel.bg/pgp/bu.asc
PGP Fingerprint: 7923 8D52 B145 02E8 6F63 8BDA 2D3F 7C0B 7AE4 5436


pgpa0VNdMvmfC.pgp
Description: PGP signature


Re: [PHP] explode a string

2005-04-18 Thread Richard Lynch
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 = $idbr /\n;
  echo \$cat = $catbr /\n;
}

 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.

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Explode a string

2003-11-12 Thread Jay Blanchard
[snip]
Any ideas how to do this,

I have a string

734088+3+734132+9+734138+80+781007+1+

I need to place the string into a multi-array like so

array[0][0] = 734088
array[0][1] = 3

etc...

Now ive tried everything i know any ideas?
[/snip]

start with explode 

$arrString = explode(+, $stringYouAreWorkingWith);

Then loop through it, placing the pairs into two-dimensional array you
mention above

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Explode a string

2003-11-12 Thread Chris W. Parker
Erin mailto:[EMAIL PROTECTED]
on Wednesday, November 12, 2003 9:13 AM said:

 734088+3+734132+9+734138+80+781007+1+
 
 I need to place the string into a multi-array like so
 
 array[0][0] = 734088
 array[0][1] = 3
[snip]
 Now ive tried everything i know any ideas?

Yes. You need to somehow differentiate between the different columns and
rows. What I suggest you do is change every other + to something else
like a ; and then split on that.

Without knowing any other way to do this I would use a regex to skip the
first + and change second one, repeating this until the end of the
string.

Then you can explode() on + and ;.


hth,
Chris.
--
Don't like reformatting your Outlook replies? Now there's relief!
http://home.in.tum.de/~jain/software/outlook-quotefix/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Explode a string

2003-11-12 Thread Chris W. Parker
Chris W. Parker 
on Wednesday, November 12, 2003 9:19 AM said:

 Without knowing any other way to do this I would use a regex to skip
 the first + and change second one, repeating this until the end of the
 string.

Considering Jay's answer for this question, do I always do things the
hard way or what??

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Explode a string

2003-11-12 Thread Jay Blanchard
[snip]
Considering Jay's answer for this question, do I always do things the
hard way or what??
[/snip]

Young Grasshopper...there is more than one way to do things, a lot of
them are rightsome are just harder than others. 

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Explode a string

2003-11-12 Thread Wouter van Vliet
Jay Blanchard wrote:
 [snip]
 Considering Jay's answer for this question, do I always do things the
 hard way or what?? [/snip]
 
 Young Grasshopper...there is more than one way to do things,
 a lot of them are rightsome are just harder than others.

Isn't that the diplomatic equivalent of yes, sometimes you tend to do the
hard way?

hehe

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php