php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45348 array_keys() convert string to integer
Submitted: 2008-06-24 11:34 UTC Modified: 2008-06-24 17:42 UTC
From: songqi at baidu dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.2.6 OS: Redhat Linux
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: songqi at baidu dot com
New email:
PHP Version: OS:

 

 [2008-06-24 11:34 UTC] songqi at baidu dot com
Description:
------------
just look at the example

Reproduce code:
---------------
<?php
$a = array('1'=>'abc','a'=>'1');
var_dump(array_keys($a));
?>

Expected result:
----------------
array(2) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "a"
}

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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-06-24 17:42 UTC] mattwil@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

No, array_keys() isn't doing that. Numeric strings used as an array key are converted to integer when the element is created. This can be seen by removing array_keys() from your code:

<?php
$a = array('1'=>'abc','a'=>'1');
var_dump($a);
?>

And you will see:

array(2) {
  [1]=>
  string(3) "abc"
  ["a"]=>
  string(1) "1"
}

(Notice no quotes around the integer key.) And NOT:

array(2) {
  ["1"]=>
  string(3) "abc"
  ["a"]=>
  string(1) "1"
}
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 16 03:01:33 2025 UTC