php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #60661 array_shift converts string-encapsulated numbers into numbers
Submitted: 2012-01-04 22:24 UTC Modified: 2012-01-24 22:52 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: jwege at gmx dot net Assigned: frozenfire (profile)
Status: Closed Package: Arrays related
PHP Version: 5.3.8 OS:
Private report: No CVE-ID: None
 [2012-01-04 22:24 UTC] jwege at gmx dot net
Description:
------------
---
From manual page: http://www.php.net/function.array-shift#refsect1-function.array-shift-seealso
---

Having an associative array where the keys are number-strings (enclosed in quotes as strings - see example), this strings get converted into numbers when using array_shift().

This is a severe bug! 

Test script:
---------------
$a["_"] = "blank dummy";
$a["01"] = "one";
$a["10"] = "ten";
$a["20"] = "twenty";

print_r( $a );
echo $a["10"] . "\n";

array_shift( $a );

print_r( $a );
echo $a["10"] . "\n"; // THIS PRODUCES AN UNDEFINED INDEX ERROR
echo $a["20"] . "\n"; // THIS PRODUCES AN UNDEFINED INDEX ERROR

echo $a[0] . "\n"; // THIS SHOULD PRODUCE AN ERROR BUT SHOWS THE VALUE
echo $a[1] . "\n"; // THIS SHOULD PRODUCE AN ERROR BUT SHOWS THE VALUE

Expected result:
----------------
I expect that the line 

echo $a["10"] . "\n";

will produce "ten" as an output, also echo $a["20"] . "\n"; should output "twenty".

Actual result:
--------------
Notice:  Undefined index: 10 in [test] on line 13

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-01-06 02:03 UTC] phpmpan at mpan dot pl
`array_shift` has nothing to do with this. Keys are integers immedietly after assignment. Use `var_dump` instad of `print_r`.
This behaviour is consistent with documentation. [1] clearly states that:
   "If a key is the standard representation of an integer,
    it will be interpreted as such."

[1] http://pl.php.net/manual/en/language.types.array.php
 [2012-01-10 10:17 UTC] asd_wte at yahoo dot com
cannot replicate the error. i get the following:
Array ( [_] => blank dummy [01] => one [10] => ten [20] => twenty ) ten Array ( 
[01] => one [0] => ten [1] => twenty ) ten twenty

must be a misconfigured php.ini
 [2012-01-24 22:52 UTC] frozenfire@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: frozenfire
 [2012-01-24 22:52 UTC] frozenfire@php.net
phpmpan at mpan dot pl is correct. This is expected behaviour that is well-
documented.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 05 12:01:32 2024 UTC