|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2011-03-17 10:10 UTC] 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] =>
)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Dec 14 11:00:02 2025 UTC |
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);