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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
41 - 6 = ?
Subscribe to this entry?

 
 [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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 13:01:29 2024 UTC