php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79525 Foreach converting/casting array keys to int
Submitted: 2020-04-26 21:31 UTC Modified: 2020-04-26 21:57 UTC
From: data dot protection dot gdpr at gmail dot com Assigned: cmb (profile)
Status: Not a bug Package: Arrays related
PHP Version: 7.3.17 OS: CentOS 6
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: data dot protection dot gdpr at gmail dot com
New email:
PHP Version: OS:

 

 [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)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-04-26 21:43 UTC] bugreports at gmail dot com
> 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"
}
 [2020-04-26 21:45 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2020-04-26 21:45 UTC] cmb@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Like bugreports@ said above.
 [2020-04-26 21:51 UTC] data dot protection dot gdpr at gmail dot com
That really doesn't seem like a good behavior, and this can definitely lead to unexpected behavior.

In PHP, we have ways to do strict comparisons (in_array(.., .., true), ===),
and honestly, this key conversion kind of defeats the purpose.

I hope that a change to this will be considered in the future.

If I'm using quotes, I want a string, not an int... I thought this was common sense.
 [2020-04-26 21:56 UTC] bugreports at gmail dot com
the problem is that it would be a *massive* and subtle BC break for millions line of code out there, anyways - you assumption that foreach is catsing or changing anything was wrong

and yes, i shoutet some dirty words into the room because of the always numeric kyes while working on 100% strict typed and strcit comparing codebase :-)
 [2020-04-26 21:57 UTC] cmb@php.net
If you want this established behavior[1] to change, you or
somebody else would have to pursue the RFC process[2].

[1] <https://3v4l.org/p9dD5>
[2] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 02:01:28 2024 UTC