php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #36391 $a[] doesn't work correctly after array_pop
Submitted: 2006-02-14 17:33 UTC Modified: 2011-04-08 21:29 UTC
From: tomas_matousek at hotmail dot com Assigned:
Status: Wont fix Package: Arrays related
PHP Version: 5.1.2 OS: WinXP
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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: tomas_matousek at hotmail dot com
New email:
PHP Version: OS:

 

 [2006-02-14 17:33 UTC] tomas_matousek at hotmail dot com
Description:
------------
Operator $a[] doesn't add a correct key to the array if applied after popping from array. It seems array_pop merely checks whether the removed index is the max. int key remembered by the array and if yes it decreases it by 1. I would expect array_pop to find the new maximal key in the resulting array.

Reproduce code:
---------------
$a = array("a","b",100 => "c",200 => "d");
array_pop($a);
array_pop($a);
$a[] = "new";
var_dump($a);

Expected result:
----------------
array(3) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(3) "new"
}




Actual result:
--------------
array(3) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [199]=>
  string(3) "new"
}




Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-01-28 14:42 UTC] msaraujo@php.net
--- php-5.2.5/ext/standard/array.c	2007-11-06 11:28:21.000000000 -0200
+++ /usr/local/src/php-5.2.5/ext/standard/array.c	2008-01-28 06:35:34.000000000 -0200
@@ -1994,7 +1994,7 @@
 	     **val;			/* Value to be popped */
 	char *key = NULL;
 	int key_len = 0;
-	ulong index;
+	ulong index, new_index = 0;
 	
 	/* Get the arguments and do error-checking */
 	if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &stack) == FAILURE) {
@@ -2048,6 +2048,10 @@
 		}
 	} else if (!key_len && index >= Z_ARRVAL_PP(stack)->nNextFreeElement-1) {
 		Z_ARRVAL_PP(stack)->nNextFreeElement = Z_ARRVAL_PP(stack)->nNextFreeElement - 1;
+	}  else if (!key_len && index < Z_ARRVAL_PP(stack)->nNextFreeElement-1) {
+      		    zend_hash_internal_pointer_end(Z_ARRVAL_PP(stack));
+                    zend_hash_get_current_key_ex(Z_ARRVAL_PP(stack), &key, &key_len, &new_index, 0, NULL);
+                    Z_ARRVAL_PP(stack)->nNextFreeElement = new_index + 1;
 	}
 
 	zend_hash_internal_pointer_reset(Z_ARRVAL_PP(stack));
 [2008-01-28 14:43 UTC] msaraujo@php.net
Since there is no special notes in docs about this issue, it seems to be a feature request, but still reasonable with array manipulation formalizations.
 [2008-01-28 22:07 UTC] davidc@php.net
This is expected behavior. Workaround you "could" do is:

<?php 
$array = array('a', 'b', 'c', 200 => 'd');
array_pop($array);
array_pop($array);
$new = $array;
$new[] = 'd';
$array[] = 'd';
var_dump($new);
var_dump($array);
?>

That way you'll have your 1,2,3 keys in $new and the correctly indexed array in $array.

"Note that the maximum integer key used for this need not currently exist in the array. It simply must have existed in the array at some time since the last time the array was re-indexed. The following example illustrates: .." (You can see this link at: http://ie.php.net/types.array)

 [2011-04-08 21:29 UTC] jani@php.net
-Status: Open +Status: Wont fix -Package: Feature/Change Request +Package: Arrays related
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 15:01:28 2024 UTC