#36391 [Opn]: $a[] doesn't work correctly after array_pop

From: Date: Mon, 28 Jan 2008 22:07:25 +0000
Subject: #36391 [Opn]: $a[] doesn't work correctly after array_pop
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-121613@lists.php.net to get a copy of this message
 ID:               36391
 Updated by:       davidc@php.net
 Reported By:      tomas_matousek at hotmail dot com
 Status:           Open
 Bug Type:         Feature/Change Request
 Operating System: WinXP
 PHP Version:      5.1.2
 New Comment:

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)



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

[2008-01-28 14:43:37] 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 14:42:13] 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));

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

[2006-02-14 17:33:38] 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"
}





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


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


Thread (5 messages)

« previous php.bugs (#121613) next »