php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69125 Array numeric string as key
Submitted: 2015-02-26 13:23 UTC Modified: 2015-02-26 13:37 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: dm@php.net Assigned:
Status: Closed Package: opcache
PHP Version: 5.6.6 OS: Ubuntu 14.04
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: dm@php.net
New email:
PHP Version: OS:

 

 [2015-02-26 13:23 UTC] dm@php.net
Description:
------------
Numeric string as array key is allowed (not cast to int) if the key value is passed as constant (or possibly some other way too).
This can be achieved by casting object with numeric properties to array, but normally one cannot assign numeric string as array key.

Also, `isset($array['0'])` returns `false` even tho it exists. (possibly another bug or a feature?)

Test script:
---------------
<?php

const SZERO = '0';

$array[SZERO] = PHP_VERSION;

var_dump($array);


Expected result:
----------------
array (size=1)
  0 => string '5.6.6-1+deb.sury.org~trusty+1' (length=29)

Actual result:
--------------
array (size=1)
  '0' => string '5.6.6-1+deb.sury.org~trusty+1' (length=29)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-02-26 13:25 UTC] dm@php.net
-Package: optimizer +Package: opcache
 [2015-02-26 13:25 UTC] dm@php.net
Wrong package.
 [2015-02-26 13:37 UTC] dm@php.net
`var_dump(isset($array[SZERO]), isset($array['0']), isset($array[0]));` returns `false` for every expression.
 [2015-02-27 01:37 UTC] edgar dot r dot sandi at gmail dot com
Hi!

maybe that's not a bug, i think that the error is in the attribution of constant.

If you use:
const SZERO = 0; // as int instead string

Then the outputs of:
var_dump(isset($array[SZERO]), isset($array['0']), isset($array[0]));

Will be:
bool(true)
bool(true)
bool(true)

cheers
Edgar R. Sandi
 [2015-02-27 03:44 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=3e82816ba1a82843a352ce4050b2010fd9932dee
Log: Fixed bug #69125 (Array numeric string as key)
 [2015-02-27 03:44 UTC] laruence@php.net
-Status: Open +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 16:01:33 2024 UTC