php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75328 Unable to typecast numeric array key to string
Submitted: 2017-10-06 17:40 UTC Modified: 2017-10-06 18:28 UTC
From: langebroek at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: Irrelevant OS: Any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: langebroek at gmail dot com
New email:
PHP Version: OS:

 

 [2017-10-06 17:40 UTC] langebroek at gmail dot com
Description:
------------
Rather self explanatory; it seems impossible to have a numeric string as an array key. For the project I'm working on this is almost a necessity.

Test script:
---------------
$buf=array((string)1=>'x',"2"=>'y','3'=>'z');
#var_dump($buf);//all int
var_dump(array_keys($buf));//clarify all int

Expected result:
----------------
In at least one case I'd expect to see a string in the array key, or have an alternative method to achieve such. In some cases it is unwise to typecast at a later time.

Actual result:
--------------
array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-10-06 17:43 UTC] requinix@php.net
-Status: Open +Status: Feedback -Package: PHP Language Specification +Package: Arrays related
 [2017-10-06 17:43 UTC] requinix@php.net
> it seems impossible to have a numeric string as an array key
That is correct, and PHP takes explicit measures to make sure it cannot happen.

What is your use case?
 [2017-10-06 18:18 UTC] langebroek at gmail dot com
I'm using it to validate certain things in the data layer (SQL abstraction). 

array( 'col1', 'col2', 'col3' ) xor array( 'col1' => 'val1', 'col2' => 'val2', 'col3' => 'val3' )

It could happen that someone has a legitimate *SQL column with the name '1' but instead it being treated as an index. It's not the end of the world, I can think of an other way.

Is there any reason why PHP does this?

It'd be a shame if I were to base my validation on the fact that it ensures this and it is changed in the future. [Bug] can be closed, I spose.
 [2017-10-06 18:18 UTC] langebroek at gmail dot com
I'm using it to validate certain things in the data layer (SQL abstraction). 

array( 'col1', 'col2', 'col3' ) xor array( 'col1' => 'val1', 'col2' => 'val2', 'col3' => 'val3' )

It could happen that someone has a legitimate *SQL column with the name '1' but instead it being treated as an index. It's not the end of the world, I can think of an other way.

Is there any reason why PHP does this?

It'd be a shame if I were to base my validation on the fact that it ensures this and it is changed in the future. [Bug] can be closed, I spose.
 [2017-10-06 18:28 UTC] requinix@php.net
-Status: Feedback +Status: Not a bug
 [2017-10-06 18:28 UTC] requinix@php.net
Since arrays are naturally keyed by integers, having integers and integer strings be distinct would create havoc.  

I don't see any reason why an integer or string key should matter in your case. Just treat the key/column name like it was a string and it will act like a string.
 [2017-10-07 00:48 UTC] bradyn at bradynpoulsen dot com
I appears to be working as expected and documented.

"Strings containing valid decimal integers, unless the number is preceded by a + sign, will be cast to the integer type. E.g. the key "8" will actually be stored under 8. On the other hand "08" will not be cast, as it isn't a valid decimal integer." - https://secure.php.net/manual/language.types.array.php
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Wed Apr 08 03:01:24 2020 UTC