php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36975 natcasesort() causes array_pop() to misbehave
Submitted: 2006-04-04 21:06 UTC Modified: 2006-11-12 01:19 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: ateixeira at gmail dot com Assigned: bjori (profile)
Status: Closed Package: Arrays related
PHP Version: 5.1.2 OS: *
Private report: No CVE-ID: None
 [2006-04-04 21:06 UTC] 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
)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-04-13 14:50 UTC] crescentfreshpot at yahoo dot com
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
)
 [2006-04-13 16:45 UTC] ateixeira at gmail dot com
At first I thought array_pop was the problem as well.  However, I only experienced this error when using 'natcasesort'.  If I changed to using 'sort' instead, the error went away.  In both natcasesort and sort, the array's indices should be out of order due to the sorting, but natcasesort is the only one to give me an error.  It's strange, though, since when I run your test, I also get an error, proving your array_pop issue.  So, that probably points to it being related to array_pop and not natcasesort, but that still doesn't explain why 'sort' will work, but not 'natcasesort'.
 [2006-04-13 19:05 UTC] crescentfreshpot at yahoo dot com
sort re-indexes it's input. natcasesort/asort/arsort etc do not.
 [2006-11-12 01:19 UTC] bjori@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

Fixed in next 4.4 & 5.2 releases
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Nov 23 07:01:29 2024 UTC