Re: [PHP] array key's: which is correct?
Paul M Foster wrote: On Tue, Jun 08, 2010 at 04:44:53PM +0200, Peter Lind wrote: On 8 June 2010 16:38, Ashley Sheridan wrote: On Tue, 2010-06-08 at 10:35 -0400, Paul M Foster wrote: On Tue, Jun 08, 2010 at 09:38:58AM -0400, Robert Cummings wrote: Tanel Tammik wrote: Hi, which one is correct or "better"? $array[3] = ''; or $array['3'] = ''; $i = 7; $array[$i] = ''; or $array["$i"] = ''; Sometimes it is good to illustrate the correct answer: '1', '2' => '2', 'three' => 'three', '4.0' => '4.0', 5.0 => 5.0, ); var_dump( array_keys( $array ) ); ?> The answer is surprising (well, not really :) and certainly advocates against making literal strings of integers or manually converting a string integer to a real integer or using floating point keys. Curse you, Rob Cummings! ;-} I was stunned at the results of this. I assumed that integers cast as strings would remain strings as indexes. Not so. And then float indexes cast to ints. Argh! My advice to the original poster was slightly incorrect. But I would still encourage you to avoid enclosing variables in double-quotes unnecessarily. (And integers in single-quotes for that matter.) Paul -- Paul M. Foster The obvious way around this would be to include some sort of character in the index that can't be cast to an integer, so instead of $array[1.0] which would equate to $array[1] maybe add an underscore to make it $array['_1.0']. It's not the prettiest of solutions, but it does mean that indexes are kept as you intended, and you need only strip out the first character, although I imagine a lot of string manipulation on a large array would decrease performance. Floats in quotes are not cast to int when used as array keys. Just an FYI :) Umm, yes, you are correct. I pasted Rob's code into a test file, added some other print_r()s and such, just to look at the whole issue. I'm *still* examining the results, trying to wrap my wits around why things are done this way. If I were to hazard a guess as to the "why" of the current functionality, I would say converting an integer string to a real i nt is optimal with respect to both memory and processing when trying to find values by key. As for floating points... Due to the inability to accurately represent some floating point numbers in binary, one would often not get what one expects even when converting to a string. So maybe integer was chosen since it was more optimal than a string. Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array key's: which is correct?
On Tue, Jun 08, 2010 at 04:44:53PM +0200, Peter Lind wrote: > On 8 June 2010 16:38, Ashley Sheridan wrote: > > On Tue, 2010-06-08 at 10:35 -0400, Paul M Foster wrote: > > > >> On Tue, Jun 08, 2010 at 09:38:58AM -0400, Robert Cummings wrote: > >> > >> > Tanel Tammik wrote: > >> >> Hi, > >> >> > >> >> which one is correct or "better"? > >> >> > >> >> $array[3] = ''; > >> >> or > >> >> $array['3'] = ''; > >> >> > >> >> $i = 7; > >> >> > >> >> $array[$i] = ''; > >> >> or > >> >> $array["$i"] = ''; > >> > > >> > Sometimes it is good to illustrate the correct answer: > >> > > >> > >> > > >> > $array = array > >> > ( > >> > '1' => '1', > >> > '2' => '2', > >> > 'three' => 'three', > >> > '4.0' => '4.0', > >> > 5.0 => 5.0, > >> > ); > >> > > >> > var_dump( array_keys( $array ) ); > >> > > >> > ?> > >> > > >> > The answer is surprising (well, not really :) and certainly advocates > >> > against making literal strings of integers or manually converting a > >> > string integer to a real integer or using floating point keys. > >> > >> Curse you, Rob Cummings! ;-} > >> > >> I was stunned at the results of this. I assumed that integers cast as > >> strings would remain strings as indexes. Not so. And then float indexes > >> cast to ints. Argh! > >> > >> My advice to the original poster was slightly incorrect. But I would > >> still encourage you to avoid enclosing variables in double-quotes > >> unnecessarily. (And integers in single-quotes for that matter.) > >> > >> Paul > >> > >> -- > >> Paul M. Foster > >> > > > > > > The obvious way around this would be to include some sort of character > > in the index that can't be cast to an integer, so instead of $array[1.0] > > which would equate to $array[1] maybe add an underscore to make it > > $array['_1.0']. It's not the prettiest of solutions, but it does mean > > that indexes are kept as you intended, and you need only strip out the > > first character, although I imagine a lot of string manipulation on a > > large array would decrease performance. > > Floats in quotes are not cast to int when used as array keys. Just an FYI :) Umm, yes, you are correct. I pasted Rob's code into a test file, added some other print_r()s and such, just to look at the whole issue. I'm *still* examining the results, trying to wrap my wits around why things are done this way. Paul -- Paul M. Foster -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array key's: which is correct?
On Tue, 2010-06-08 at 17:11 +0200, Peter Lind wrote: > On 8 June 2010 16:53, Ashley Sheridan wrote: > > > > On Tue, 2010-06-08 at 16:44 +0200, Peter Lind wrote: > > > > On 8 June 2010 16:38, Ashley Sheridan wrote: > > > On Tue, 2010-06-08 at 10:35 -0400, Paul M Foster wrote: > > > > > >> On Tue, Jun 08, 2010 at 09:38:58AM -0400, Robert Cummings wrote: > > >> > > >> > Tanel Tammik wrote: > > >> >> Hi, > > >> >> > > >> >> which one is correct or "better"? > > >> >> > > >> >> $array[3] = ''; > > >> >> or > > >> >> $array['3'] = ''; > > >> >> > > >> >> $i = 7; > > >> >> > > >> >> $array[$i] = ''; > > >> >> or > > >> >> $array["$i"] = ''; > > >> > > > >> > Sometimes it is good to illustrate the correct answer: > > >> > > > >> > > >> > > > >> > $array = array > > >> > ( > > >> > '1' => '1', > > >> > '2' => '2', > > >> > 'three' => 'three', > > >> > '4.0' => '4.0', > > >> > 5.0 => 5.0, > > >> > ); > > >> > > > >> > var_dump( array_keys( $array ) ); > > >> > > > >> > ?> > > >> > > > >> > The answer is surprising (well, not really :) and certainly advocates > > >> > against making literal strings of integers or manually converting a > > >> > string integer to a real integer or using floating point keys. > > >> > > >> Curse you, Rob Cummings! ;-} > > >> > > >> I was stunned at the results of this. I assumed that integers cast as > > >> strings would remain strings as indexes. Not so. And then float indexes > > >> cast to ints. Argh! > > >> > > >> My advice to the original poster was slightly incorrect. But I would > > >> still encourage you to avoid enclosing variables in double-quotes > > >> unnecessarily. (And integers in single-quotes for that matter.) > > >> > > >> Paul > > >> > > >> -- > > >> Paul M. Foster > > >> > > > > > > > > > The obvious way around this would be to include some sort of character > > > in the index that can't be cast to an integer, so instead of $array[1.0] > > > which would equate to $array[1] maybe add an underscore to make it > > > $array['_1.0']. It's not the prettiest of solutions, but it does mean > > > that indexes are kept as you intended, and you need only strip out the > > > first character, although I imagine a lot of string manipulation on a > > > large array would decrease performance. > > > > Floats in quotes are not cast to int when used as array keys. Just an FYI :) > > > > Regards > > Peter > > > > > > They are. Go look at Robs earlier example. Even building upon that to make > > a float value where it doesn't equate to an integer, it is still cast as an > > integer unless it's inside a string: > > > > $array = array > > ( > > '1' => '1', > > '2' => '2', > > 'three' => 'three', > > '4.0' => '4.0', > > 5.0 => 5.0, > > 6.5 => 6.5, > > ); > > > > var_dump( array_keys( $array ) ); > > > > That's Robs code, but I added in the last element to show how a float index > > is converted to an integer. Putting the float value inside a string solves > > the issue. > > > > Did you read what I wrote? > > > ***Floats in quotes*** are not cast to int when used as array keys. Just an > > FYI :) > > I tested Robs example, that's how I know that floats in quotes are not > converted to ints, whether or not you use '4.0' or '6.5' > > Regards > Peter > > -- > > WWW: http://plphp.dk / http://plind.dk > LinkedIn: http://www.linkedin.com/in/plind > BeWelcome/Couchsurfing: Fake51 > Twitter: http://twitter.com/kafe15 > Sorry, my bad, I misread your email, you were right! Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] array key's: which is correct?
On 8 June 2010 16:53, Ashley Sheridan wrote: > > On Tue, 2010-06-08 at 16:44 +0200, Peter Lind wrote: > > On 8 June 2010 16:38, Ashley Sheridan wrote: > > On Tue, 2010-06-08 at 10:35 -0400, Paul M Foster wrote: > > > >> On Tue, Jun 08, 2010 at 09:38:58AM -0400, Robert Cummings wrote: > >> > >> > Tanel Tammik wrote: > >> >> Hi, > >> >> > >> >> which one is correct or "better"? > >> >> > >> >> $array[3] = ''; > >> >> or > >> >> $array['3'] = ''; > >> >> > >> >> $i = 7; > >> >> > >> >> $array[$i] = ''; > >> >> or > >> >> $array["$i"] = ''; > >> > > >> > Sometimes it is good to illustrate the correct answer: > >> > > >> > >> > > >> > $array = array > >> > ( > >> > '1' => '1', > >> > '2' => '2', > >> > 'three' => 'three', > >> > '4.0' => '4.0', > >> > 5.0 => 5.0, > >> > ); > >> > > >> > var_dump( array_keys( $array ) ); > >> > > >> > ?> > >> > > >> > The answer is surprising (well, not really :) and certainly advocates > >> > against making literal strings of integers or manually converting a > >> > string integer to a real integer or using floating point keys. > >> > >> Curse you, Rob Cummings! ;-} > >> > >> I was stunned at the results of this. I assumed that integers cast as > >> strings would remain strings as indexes. Not so. And then float indexes > >> cast to ints. Argh! > >> > >> My advice to the original poster was slightly incorrect. But I would > >> still encourage you to avoid enclosing variables in double-quotes > >> unnecessarily. (And integers in single-quotes for that matter.) > >> > >> Paul > >> > >> -- > >> Paul M. Foster > >> > > > > > > The obvious way around this would be to include some sort of character > > in the index that can't be cast to an integer, so instead of $array[1.0] > > which would equate to $array[1] maybe add an underscore to make it > > $array['_1.0']. It's not the prettiest of solutions, but it does mean > > that indexes are kept as you intended, and you need only strip out the > > first character, although I imagine a lot of string manipulation on a > > large array would decrease performance. > > Floats in quotes are not cast to int when used as array keys. Just an FYI :) > > Regards > Peter > > > They are. Go look at Robs earlier example. Even building upon that to make a > float value where it doesn't equate to an integer, it is still cast as an > integer unless it's inside a string: > > $array = array > ( > '1' => '1', > '2' => '2', > 'three' => 'three', > '4.0' => '4.0', > 5.0 => 5.0, > 6.5 => 6.5, > ); > > var_dump( array_keys( $array ) ); > > That's Robs code, but I added in the last element to show how a float index > is converted to an integer. Putting the float value inside a string solves > the issue. > Did you read what I wrote? > ***Floats in quotes*** are not cast to int when used as array keys. Just an > FYI :) I tested Robs example, that's how I know that floats in quotes are not converted to ints, whether or not you use '4.0' or '6.5' Regards Peter -- WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind BeWelcome/Couchsurfing: Fake51 Twitter: http://twitter.com/kafe15 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array key's: which is correct?
On Tue, 2010-06-08 at 16:44 +0200, Peter Lind wrote: > On 8 June 2010 16:38, Ashley Sheridan wrote: > > On Tue, 2010-06-08 at 10:35 -0400, Paul M Foster wrote: > > > >> On Tue, Jun 08, 2010 at 09:38:58AM -0400, Robert Cummings wrote: > >> > >> > Tanel Tammik wrote: > >> >> Hi, > >> >> > >> >> which one is correct or "better"? > >> >> > >> >> $array[3] = ''; > >> >> or > >> >> $array['3'] = ''; > >> >> > >> >> $i = 7; > >> >> > >> >> $array[$i] = ''; > >> >> or > >> >> $array["$i"] = ''; > >> > > >> > Sometimes it is good to illustrate the correct answer: > >> > > >> > >> > > >> > $array = array > >> > ( > >> > '1' => '1', > >> > '2' => '2', > >> > 'three' => 'three', > >> > '4.0' => '4.0', > >> > 5.0 => 5.0, > >> > ); > >> > > >> > var_dump( array_keys( $array ) ); > >> > > >> > ?> > >> > > >> > The answer is surprising (well, not really :) and certainly advocates > >> > against making literal strings of integers or manually converting a > >> > string integer to a real integer or using floating point keys. > >> > >> Curse you, Rob Cummings! ;-} > >> > >> I was stunned at the results of this. I assumed that integers cast as > >> strings would remain strings as indexes. Not so. And then float indexes > >> cast to ints. Argh! > >> > >> My advice to the original poster was slightly incorrect. But I would > >> still encourage you to avoid enclosing variables in double-quotes > >> unnecessarily. (And integers in single-quotes for that matter.) > >> > >> Paul > >> > >> -- > >> Paul M. Foster > >> > > > > > > The obvious way around this would be to include some sort of character > > in the index that can't be cast to an integer, so instead of $array[1.0] > > which would equate to $array[1] maybe add an underscore to make it > > $array['_1.0']. It's not the prettiest of solutions, but it does mean > > that indexes are kept as you intended, and you need only strip out the > > first character, although I imagine a lot of string manipulation on a > > large array would decrease performance. > > Floats in quotes are not cast to int when used as array keys. Just an FYI :) > > Regards > Peter > They are. Go look at Robs earlier example. Even building upon that to make a float value where it doesn't equate to an integer, it is still cast as an integer unless it's inside a string: $array = array ( '1' => '1', '2' => '2', 'three' => 'three', '4.0' => '4.0', 5.0 => 5.0, 6.5=> 6.5, ); var_dump( array_keys( $array ) ); That's Robs code, but I added in the last element to show how a float index is converted to an integer. Putting the float value inside a string solves the issue. Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] array key's: which is correct?
On 8 June 2010 16:38, Ashley Sheridan wrote: > On Tue, 2010-06-08 at 10:35 -0400, Paul M Foster wrote: > >> On Tue, Jun 08, 2010 at 09:38:58AM -0400, Robert Cummings wrote: >> >> > Tanel Tammik wrote: >> >> Hi, >> >> >> >> which one is correct or "better"? >> >> >> >> $array[3] = ''; >> >> or >> >> $array['3'] = ''; >> >> >> >> $i = 7; >> >> >> >> $array[$i] = ''; >> >> or >> >> $array["$i"] = ''; >> > >> > Sometimes it is good to illustrate the correct answer: >> > >> > > > >> > $array = array >> > ( >> > '1' => '1', >> > '2' => '2', >> > 'three' => 'three', >> > '4.0' => '4.0', >> > 5.0 => 5.0, >> > ); >> > >> > var_dump( array_keys( $array ) ); >> > >> > ?> >> > >> > The answer is surprising (well, not really :) and certainly advocates >> > against making literal strings of integers or manually converting a >> > string integer to a real integer or using floating point keys. >> >> Curse you, Rob Cummings! ;-} >> >> I was stunned at the results of this. I assumed that integers cast as >> strings would remain strings as indexes. Not so. And then float indexes >> cast to ints. Argh! >> >> My advice to the original poster was slightly incorrect. But I would >> still encourage you to avoid enclosing variables in double-quotes >> unnecessarily. (And integers in single-quotes for that matter.) >> >> Paul >> >> -- >> Paul M. Foster >> > > > The obvious way around this would be to include some sort of character > in the index that can't be cast to an integer, so instead of $array[1.0] > which would equate to $array[1] maybe add an underscore to make it > $array['_1.0']. It's not the prettiest of solutions, but it does mean > that indexes are kept as you intended, and you need only strip out the > first character, although I imagine a lot of string manipulation on a > large array would decrease performance. Floats in quotes are not cast to int when used as array keys. Just an FYI :) Regards Peter -- WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind BeWelcome/Couchsurfing: Fake51 Twitter: http://twitter.com/kafe15 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array key's: which is correct?
On Tue, 2010-06-08 at 10:35 -0400, Paul M Foster wrote: > On Tue, Jun 08, 2010 at 09:38:58AM -0400, Robert Cummings wrote: > > > Tanel Tammik wrote: > >> Hi, > >> > >> which one is correct or "better"? > >> > >> $array[3] = ''; > >> or > >> $array['3'] = ''; > >> > >> $i = 7; > >> > >> $array[$i] = ''; > >> or > >> $array["$i"] = ''; > > > > Sometimes it is good to illustrate the correct answer: > > > > > > > $array = array > > ( > > '1' => '1', > > '2' => '2', > > 'three' => 'three', > > '4.0' => '4.0', > > 5.0 => 5.0, > > ); > > > > var_dump( array_keys( $array ) ); > > > > ?> > > > > The answer is surprising (well, not really :) and certainly advocates > > against making literal strings of integers or manually converting a > > string integer to a real integer or using floating point keys. > > Curse you, Rob Cummings! ;-} > > I was stunned at the results of this. I assumed that integers cast as > strings would remain strings as indexes. Not so. And then float indexes > cast to ints. Argh! > > My advice to the original poster was slightly incorrect. But I would > still encourage you to avoid enclosing variables in double-quotes > unnecessarily. (And integers in single-quotes for that matter.) > > Paul > > -- > Paul M. Foster > The obvious way around this would be to include some sort of character in the index that can't be cast to an integer, so instead of $array[1.0] which would equate to $array[1] maybe add an underscore to make it $array['_1.0']. It's not the prettiest of solutions, but it does mean that indexes are kept as you intended, and you need only strip out the first character, although I imagine a lot of string manipulation on a large array would decrease performance. Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] array key's: which is correct?
On Tue, Jun 08, 2010 at 09:38:58AM -0400, Robert Cummings wrote: > Tanel Tammik wrote: >> Hi, >> >> which one is correct or "better"? >> >> $array[3] = ''; >> or >> $array['3'] = ''; >> >> $i = 7; >> >> $array[$i] = ''; >> or >> $array["$i"] = ''; > > Sometimes it is good to illustrate the correct answer: > > > $array = array > ( > '1' => '1', > '2' => '2', > 'three' => 'three', > '4.0' => '4.0', > 5.0 => 5.0, > ); > > var_dump( array_keys( $array ) ); > > ?> > > The answer is surprising (well, not really :) and certainly advocates > against making literal strings of integers or manually converting a > string integer to a real integer or using floating point keys. Curse you, Rob Cummings! ;-} I was stunned at the results of this. I assumed that integers cast as strings would remain strings as indexes. Not so. And then float indexes cast to ints. Argh! My advice to the original poster was slightly incorrect. But I would still encourage you to avoid enclosing variables in double-quotes unnecessarily. (And integers in single-quotes for that matter.) Paul -- Paul M. Foster -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array key's: which is correct?
On Tue, 2010-06-08 at 09:38 -0400, Robert Cummings wrote: > Tanel Tammik wrote: > > Hi, > > > > which one is correct or "better"? > > > > $array[3] = ''; > > or > > $array['3'] = ''; > > > > $i = 7; > > > > $array[$i] = ''; > > or > > $array["$i"] = ''; > > Sometimes it is good to illustrate the correct answer: > > > $array = array > ( > '1' => '1', > '2' => '2', > 'three' => 'three', > '4.0' => '4.0', > 5.0 => 5.0, > ); > > var_dump( array_keys( $array ) ); > > ?> > > The answer is surprising (well, not really :) and certainly advocates > against making literal strings of integers or manually converting a > string integer to a real integer or using floating point keys. > > Cheers, > Rob. > -- > E-Mail Disclaimer: Information contained in this message and any > attached documents is considered confidential and legally protected. > This message is intended solely for the addressee(s). Disclosure, > copying, and distribution are prohibited unless authorized. > Yeah, I found that out the hard way when I was trying to make an array of Gantt tasks, and realised that all my nice task numbers were changed! Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] array key's: which is correct?
Tanel Tammik wrote: Hi, which one is correct or "better"? $array[3] = ''; or $array['3'] = ''; $i = 7; $array[$i] = ''; or $array["$i"] = ''; Sometimes it is good to illustrate the correct answer: '1', '2' => '2', 'three' => 'three', '4.0' => '4.0', 5.0 => 5.0, ); var_dump( array_keys( $array ) ); ?> The answer is surprising (well, not really :) and certainly advocates against making literal strings of integers or manually converting a string integer to a real integer or using floating point keys. Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array key's: which is correct?
On Tue, 2010-06-08 at 16:12 +0300, Tanel Tammik wrote: > Hi, > > which one is correct or "better"? > > $array[3] = ''; > or > $array['3'] = ''; > > $i = 7; > > $array[$i] = ''; > or > $array["$i"] = ''; > > > Br > Tanel > > > The two indexes are equivalent, although I reckon the integer one will give better performance over the string. Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] array key's: which is correct?
On Tue, Jun 08, 2010 at 04:12:42PM +0300, Tanel Tammik wrote: > Hi, > > which one is correct or "better"? > > $array[3] = ''; > or > $array['3'] = ''; If the index for (integer) 3, the first example is correct. If the index is (string) '3', the second example is correct. > > $i = 7; > > $array[$i] = ''; > or > $array["$i"] = ''; > There's no reason to use "$i". The end result will be the same, but in the case of "$i", you're forcing the PHP interpreter to interpret the string "$i", looking for variables (like $i), and output whatever else is in the string (which in this case is nothing). Also, if $i is an integer, you have the same problem as above. In the first case, you get $array[7]. In the second case, you get $array['7']. Paul -- Paul M. Foster -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] array key's: which is correct?
Hi, which one is correct or "better"? $array[3] = ''; or $array['3'] = ''; $i = 7; $array[$i] = ''; or $array["$i"] = ''; Br Tanel -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php