php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54288 array_unshift and multidimension arrays bug
Submitted: 2011-03-17 10:10 UTC Modified: 2011-03-17 10:18 UTC
From: piotrekkr at piotrekkr dot info Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.3SVN-2011-03-17 (SVN) OS: Ubuntu 10.10
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: piotrekkr at piotrekkr dot info
New email:
PHP Version: OS:

 

 [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] => 
)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-03-17 10:18 UTC] scottmac@php.net
-Status: Open +Status: Bogus
 [2011-03-17 10:18 UTC] 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 11:14 UTC] piotrekkr at piotrekkr dot info
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 ?
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 06:01:34 2025 UTC