#30768 [Opn]: array_unshift does not honor key types

From: Date: Fri, 12 Nov 2004 13:34:52 +0000
Subject: #30768 [Opn]: array_unshift does not honor key types
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-68986@lists.php.net to get a copy of this message
ID: 30768 User updated by: olito24 at gmx dot de Reported By: olito24 at gmx dot de Status: Open Bug Type: Arrays related Operating System: debian linux 2.4.21-1-686 PHP Version: 4.3.9 New Comment: arrays don't take numerical keys as strings, they are always casted into integers. so that's the problem but not a bug. Previous Comments: ------------------------------------------------------------------------ [2004-11-12 14:15:39] 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 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=30768&edit=1

« previous php.bugs (#68986) next »