|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2020-04-26 21:31 UTC] data dot protection dot gdpr at gmail dot com
Description:
------------
I have an array with "numeric" key as string (e.g. '1'), intentionally, as semantically it makes sense for the context.
However, I noticed that using a foreach will convert the numeric string keys to "int", which then causes problems with strict comparisons, for example.
This does not seem like a good behavior, and I wonder if this should change.
Test script:
---------------
<?php
$array = array(
'' => 'test',
'1' => 'test1',
'5' => 'test5',
'2' => 'test2',
'10' => 'test10'
);
foreach ($array as $key => $val) {
var_dump($key);
}
Expected result:
----------------
string(0) ""
string(1) "1"
string(1) "5"
string(1) "2"
string(2) "10"
Actual result:
--------------
string(0) ""
int(1)
int(5)
int(2)
int(10)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 20:00:01 2025 UTC |
> I have an array with "numeric" key as string (e.g. '1') you don't, you just think you do php > var_dump([1=>'a', 2=>'b']); array(2) { [1]=> string(1) "a" [2]=> string(1) "b" } php > var_dump(['1'=>'a', '2'=>'b']); array(2) { [1]=> string(1) "a" [2]=> string(1) "b" }