ID:               36975
 Comment by:       crescentfreshpot at yahoo dot com
 Reported By:      ateixeira at gmail dot com
 Status:           Open
 Bug Type:         Arrays related
 Operating System: *
 PHP Version:      5.1.3RC3
 New Comment:

This seems like an array_pop() bug to me.

This behaviour happens when array indices are 'out of order' and you
then use array_pop(). Any subsequent 'pushing' onto the end of the
array fails.

Using either the empty-square-bracket syntax or array_push() will both
fail. Square-bracket syntax issues the warning, array_push() does not.


Re: pushing new values onto the end of an array, the docs state: "the
maximum of the existing integer indices is taken, and the new key will
be that maximum value + 1"

It seems the engine cannot generate the next index after array_pop()
does it's thing.


Smaller reproduce code:
<?php
error_reporting(E_ALL);
$list = array(1 => 'foo', 0 => 'baz');
array_pop($list);
$list[] = 'bar'; // <- fails, issues warning
array_push($list, 'bar'); // <- fails, no warning
print_r($list);
?>


Expected Result
---------------
Array
(
    [1] => foo
    [2] => bar
    [3] => bar
)


Actual Result
-------------
Warning: Cannot add element to the array as the next element is already
occupied in C:\Dev\webserver_root\t.php on line 5
Array
(
    [1] => foo
)


Previous Comments:
------------------------------------------------------------------------

[2006-04-04 21:06:37] ateixeira at gmail dot com

Description:
------------
After using natcasesort, using the array_pop and array_push (or arr[] =
$value construct) in conjunction cause the following error (on
array_push):

Warning:  Cannot add element to the array as the next element is
already occupied in xxxxx.php on Line xx

Also, the value doesn't get pushed onto the array.

Reproduce code:
---------------
$fileList = array('aa', 'aa', 'bb', 'bb', 'cc', 'cc');                 
        
$test = natcasesort($fileList);                                        
        
                                                                       
        
if ($test) {                                                           
        
  echo "natcasesort success!\n";                                       
        
}                                                                      
        
                                                                       
        
$val = array_pop($fileList);                                           
        
$fileList[] = $val;                                                    
        
                                                                       
        
print_r($fileList);


Expected result:
----------------
natcasesort success!
Array
(
    [0] => aa
    [1] => aa
    [2] => bb
    [3] => bb
    [4] => cc
    [5] => cc
)


Actual result:
--------------
natcasesort success!
PHP Warning:  Cannot add element to the array as the next element is
already occupied in /webroot/root/projects/RPMfe/j.php on line 10

Warning: Cannot add element to the array as the next element is already
occupied in /webroot/root/projects/RPMfe/j.php on line 10
Array
(
    [0] => aa
    [1] => aa
    [3] => bb
    [2] => bb
    [5] => cc
)


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=36975&edit=1

Reply via email to