php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #30768 array_unshift does not honor key types
Submitted: 2004-11-12 14:15 UTC Modified: 2004-12-10 20:06 UTC
From: olito24 at gmx dot de Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 4.3.9 OS: debian linux 2.4.21-1-686
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: olito24 at gmx dot de
New email:
PHP Version: OS:

 

 [2004-11-12 14:15 UTC] olito24 at gmx dot de
Description:
------------
the array that is to be passed to array_unshift() has strings as its keys. if a key is a number, array_unshift does not keep the type string for this key in the resulting array. in the following example, the top level key [1] should in fact be [181050] (as a string). it is index [1] because the first entry is [0], which in fact is meant to be an integer value. an example how i used unshift to insert the first entry in the already existing array:

array_unshift ($relativeCampaignData_A, array ('id' => 0, 'title' => 'k.A.'));

(usually the [id] entries are strings, the first top array element is an exception.

the resulting array is:

Array
(
    [0] => Array
        (
            [id] => 0
            [title] => k.A.
        )

    [e94a5a] => Array
        (
            [id] => e94a5a
            [title] => example1
        )

    [0f298f] => Array
        (
            [id] => 0f298f
            [title] => example2
        )

    [1] => Array
        (
            [id] => 181050
            [title] => thisKeyIsTheError
        )

    [1cfb8b] => Array
        (
            [id] => 1cfb8b
            [title] => example3
        )

)

before using unshift index [1] was associative index [181050] as a string by using strval.

Reproduce code:
---------------
$array = array('123abc' => array('id' = '123abc', 'title' = 'example2'), '456789' => array('id' = '456789', 'title' = 'errorexample'));

print_r ($array);
echo '<br><br>';

array_unshift ($array, array('id' => 0, 'title' => 'example1'));

print_r ($array);

Expected result:
----------------
$array [0] = array
$array ['123abc'] = array
$array ['456789'] = array

Actual result:
--------------
$array [0] = array
$array ['123abc'] = array
$array [1] = array

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-11-12 14:34 UTC] olito24 at gmx dot de
arrays don't take numerical keys as strings, they are always casted into integers. so that's the problem but not a bug.
 [2004-12-10 20:06 UTC] nlopess@php.net
Yep, you are right. and the manual states:
"All numerical array keys will be modified to start counting from zero while literal keys won't be touched."

So there is no problem in the documentation. closing..
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 11:01:32 2025 UTC