php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #8325 numeric strings used as keys are converted to longs
Submitted: 2000-12-19 15:54 UTC Modified: 2003-08-10 20:16 UTC
From: cynic at mail dot cz Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 4.0 Latest CVS (19/12/2000) OS: *
Private report: No CVE-ID:
 [2000-12-19 15:54 UTC] cynic at mail dot cz
(Actually, this is for PHP 4 versions up to 4.0.4 RC6, but it ain't in the version list.)

I don't want to sound harsh or something, and this behavior seemed to be accepted as feature from what I saw elsewhere in the bug database (can't find it, though), but I think it shouldn't be that way. 

Current behavior:

<?
$p = 1 ;
settype( $p , 'string' ) ;
$a = array( $p => 'aaaa' ) ;
var_dump( $a ) ;
?>

output (note: casting within the array() doesn't help):
string(1) "1"
array(1) {
  [1]=>
  string(4) "aaaa"
}


'Right' (YMMV) behavior:
string(1) "1"
array(1) {
  ["1"]=>
  string(4) "aaaa"
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-02-08 12:36 UTC] moriyoshi@php.net
Related to bug #21918

 [2003-08-10 20:16 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Indexes with numerical vales (zero or positive) are casted to integer.
 [2004-05-19 02:15 UTC] alternateduh at hotmail dot com
Just a question...  Why is this the decided upon behavior?  Is there any way to implement a flexible/variable "struct"-like data structure with the ability to have numeric strings ("8") that aren't converted to ints/longs?  This unfortunately is now a serious issue for my company, and I'm pulling out my hair! :( any help/advice?
 [2004-05-19 02:31 UTC] papercrane at reversefold dot com
Why exactly is this important? You can still access the array fine can't you?

$arr["8"] = 2;
echo $arr["8"];

That works fine for me.

Perhaps you have numbers longer than that that it's choking on? It seems that if you add a 0 at the beginning, PHP will leave it as a string.

$arr["08"] = 2;
var_dump($arr);
 [2004-05-19 02:38 UTC] alternateduh at hotmail dot com
Actually it is very important for us for some database and xmlrpc code that depends on critical data types that cannot change types.  As the strings go to ints, the code later believes that the indicies are ints instead of strings, and unfortunately sql syntaxes and xmlrpc data types no longer match. :(
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Sat Apr 19 12:01:52 2014 UTC