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.2 New Comment:
sort re-indexes it's input. natcasesort/asort/arsort etc do not. Previous Comments: ------------------------------------------------------------------------ [2006-04-13 16:45:54] 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 14:50:19] 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-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