php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #24652 array keys that are strings but numeric behave incorrectly
Submitted: 2003-07-14 16:53 UTC Modified: 2003-08-04 03:42 UTC
Votes:3
Avg. Score:4.7 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:1 (50.0%)
From: tater at potatoe dot com Assigned: sterling
Status: Closed Package: Scripting Engine problem
PHP Version: 5CVS-2003-07-14 (dev) OS: *
Private report: No CVE-ID:
 [2003-07-14 16:53 UTC] tater at potatoe dot com
Description:
------------
This is really bug #23935, but that's marked 'Closed' and I am still seeing the problem in the current PHP5 code. It's beyond array_flip(), too, strictly. I first saw this in $_POST with a field named "myfield[1]" - this comes through as $_POST["myfield"]["1"]. Same with array_combine() using array("1") as the key array.

Reproduce code:
---------------
submit a form with hidden field named 'myfield[1]':
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="myfield[1]" value="1" >
<input type="submit">
</form>
<?php
function testit($a)
{
        static $tick = 1;
        echo "<li>test ", $tick++, ":\n";
        var_dump($a, array_key_exists(key($a), $a), empty($a[1]), empty($a['1']));
}
testit(array("1"=>"1"));
testit(array_flip(array(1=>"1")));
testit(array_combine(array('1'),array('1')));
if (!empty($_POST['myfield'])) { testit($_POST['myfield']); }
?>

Expected result:
----------------
Output for test 2 on PHP 4.3.3RC1 (cli) (built: Jun 20 2003 23:09:34) (DEBUG):

array(1) {
  [1]=>
  int(1)
}
bool(true)
bool(false)
bool(false)

Actual result:
--------------
Output for test 2 on PHP 5.0.0b2-dev (cli) (built: Jul 14 2003 14:14:54) (DEBUG):

array(1) {
  ["1"]=>
  int(1)
}
bool(false)
bool(true)
bool(true)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-07-17 23:07 UTC] sniper@php.net
The OS is irrelevant.
This simple script shows that there indeed is a bug:

<?php
  /* This works */
  $f = array('7' => 0);
  var_dump($f);
  var_dump(array_key_exists(7, $f));
  var_dump(array_key_exists('7', $f));

  /* This doesn't */
  $f = array_flip(array('7'));
  var_dump($f);
  var_dump(array_key_exists(7, $f));
  var_dump(array_key_exists('7', $f));
?>

Result with PHP 4.3.3RC2-dev:
-----------------------------
array(1) {
  [7]=>
  int(0)
}
bool(true)
bool(true)
array(1) {
  [7]=>
  int(0)
}
bool(true)
bool(true)

Result with PHP 5.0.0b2-dev:
---------------------
array(1) {
  [7]=>
  int(0)
}
bool(true)
bool(true)
array(1) {
  ["7"]=>
  int(0)
}
bool(false)
bool(false)

Assigning to Sterling who broke it. :)

 [2003-08-04 03:42 UTC] zeev@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Fri Apr 18 05:03:21 2014 UTC