Bug #79525 [NEW]: Foreach converting/casting array keys to int
| From: | data dot protection dot gdpr at gmail dot com | Date: | Sun, 26 Apr 2020 21:31:59 +0000 |
| Subject: | Bug #79525 [NEW]: Foreach converting/casting array keys to int | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-226781@lists.php.net to get a copy of this message | ||
From: data dot protection dot gdpr at gmail dot com
Operating system: CentOS 6
PHP version: 7.3.17
Package: Arrays related
Bug Type: Bug
Bug description:Foreach converting/casting array keys to int
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)
--
Edit bug report at https://bugs.php.net/bug.php?id=79525&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=79525&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=79525&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=79525&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=79525&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=79525&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=79525&r=support
Expected behavior: https://bugs.php.net/fix.php?id=79525&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=79525&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=79525&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=79525&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=79525&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=79525&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=79525&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=79525&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=79525&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=79525&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=79525&r=mysqlcfg