php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22201 Array keys like "-123" are not turned into ints
Submitted: 2003-02-13 05:26 UTC Modified: 2003-02-13 06:53 UTC
From: carl at thep dot lu dot se Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 4.3.0 OS: SuSE 7.2
Private report: No CVE-ID: None
 [2003-02-13 05:26 UTC] carl at thep dot lu dot se
A string which would evaluate to a negative integer (such as "-123")
will not be turned into an integer when used as an array key. I don't
recall seeing anywhere an explicit explanation of how strings are
treated when used as array keys, but obviously those strings that
are valid representations of non-negative integers are implicitly
treated as integers, and everything else is seen as strings.
I think it would make sense to extend this to _all_ valid integers, as
the behavior in this example is not quite what one would expect:

$arr = array(-1 => "a", 0 => "b", 1 => "c");
$arr["-1"] = "e";
$arr["0"] = "f";
$arr["1"] = "g";
print_r($arr);
echo "\n";
var_dump($arr);

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-02-13 06:53 UTC] moriyoshi@php.net
Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same. Because of this, we hope you add your comments
to the existing bug instead.

Thank you for your interest in PHP.

dupe of bug #21918
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Mon Nov 23 17:01:23 2020 UTC