Bug #54288 [Com]: array_unshift and multidimension arrays bug
| From: | piotrekkr at piotrekkr dot info | Date: | Thu, 17 Mar 2011 10:14:57 +0000 |
| Subject: | Bug #54288 [Com]: array_unshift and multidimension arrays bug | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-159547@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=54288&edit=1
ID: 54288
Comment by: piotrekkr at piotrekkr dot info
Reported by: piotrekkr at piotrekkr dot info
Summary: array_unshift and multidimension arrays bug
Status: Bogus
Type: Bug
Package: Arrays related
Operating System: Ubuntu 10.10
PHP Version: 5.3SVN-2011-03-17 (SVN)
Block user comment: N
Private report: N
New Comment:
I know that if param passed by reference does not exists it is treated
as null.
But the problem is: why aray_unshift() treats not existing index $arr[4]
as $arr? Why it appends null at the end of $arr ?
Previous Comments:
------------------------------------------------------------------------
[2011-03-17 10:18:53] scottmac@php.net
This isn't related to array_unshift, it happens to any function which
takes a
parameter by reference. When you pass in a non existent value as a
reference PHP
sets it to NULL. Else you'd have nothing to work with.
<?php
function my_func(&$test) {
return false;
}
$arr = array(
array('ddddd'),
array('eeee'),
array('ffff'),
array('gggg'),
);
my_func($arr[4]);
print_r($arr);
------------------------------------------------------------------------
[2011-03-17 10:10:44] piotrekkr at piotrekkr dot info
Description:
------------
When using array_unshift() on multidimension arrays there is a bug when
index in array does not exists. When you have array with arrays inside
like this:
$arr = array(
array('ddddd'),
array('eeee'),
array('ffff'),
array('gggg'),
);
and you use:
array_unshift($arr[4], 4);
this function not only show you warning message, but also add one
element with null value to the end (!) of $arr array. It should not be
working like that. It should show a warning and do nothing.
My version of PHP:
piotrekkr@piotrekkr-desktop:/var/www$ php -v
PHP 5.3.3-1ubuntu9.3 with Suhosin-Patch (cli) (built: Jan 12 2011
16:07:38)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
OS: Ubuntu 10.10
Test script:
---------------
<?php
$arr = array(
array('ddddd'),
array('eeee'),
array('ffff'),
array('gggg'),
);
array_unshift($arr[4], 4);
print_r($arr);
Expected result:
----------------
PHP Warning: array_unshift() expects parameter 1 to be array, null
given in /var/www/unshift_test.php on line 9
Array
(
[0] => Array
(
[0] => ddddd
)
[1] => Array
(
[0] => eeee
)
[2] => Array
(
[0] => ffff
)
[3] => Array
(
[0] => gggg
)
)
Actual result:
--------------
PHP Warning: array_unshift() expects parameter 1 to be array, null
given in /var/www/unshift_test.php on line 9
Array
(
[0] => Array
(
[0] => ddddd
)
[1] => Array
(
[0] => eeee
)
[2] => Array
(
[0] => ffff
)
[3] => Array
(
[0] => gggg
)
[4] =>
)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=54288&edit=1