php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69166 Assigning array_values() to array does not reset key counter
Submitted: 2015-03-03 07:52 UTC Modified: 2015-03-03 09:02 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: cweiske@php.net Assigned:
Status: Closed Package: Arrays related
PHP Version: master-Git-2015-03-03 (Git) OS:
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: cweiske@php.net
New email:
PHP Version: OS:

 

 [2015-03-03 07:52 UTC] cweiske@php.net
Description:
------------
When assigning the result of array_values() to an array variable, its internal key counter does not get reset anymore as it did on PHP 4 and PHP 5.

Test script:
---------------
<?php
$ar = array('foo');
unset($ar[0]);
$ar = array_values($ar);
$ar[] = 'bar';
var_dump($ar);
?>

Expected result:
----------------
array(1) {
  [0] =>
  string(3) "bar"
}


Actual result:
--------------
array(1) {
  [1]=>
  string(3) "bar"
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-03-03 07:56 UTC] cweiske@php.net
Also see http://3v4l.org/s5fJX
 [2015-03-03 09:02 UTC] reeze@php.net
This should works:

diff --git a/Zend/zend_hash.h b/Zend/zend_hash.h
index afa3602..30e0453 100644
--- a/Zend/zend_hash.h
+++ b/Zend/zend_hash.h
@@ -795,7 +795,7 @@ static zend_always_inline void *zend_hash_get_current_data_ptr_ex(HashTable *ht,
 #define ZEND_HASH_FILL_END() \
 		__fill_ht->nNumUsed = __fill_idx; \
 		__fill_ht->nNumOfElements = __fill_idx; \
-		__fill_ht->nNextFreeElement = __fill_idx + 1; \
+		__fill_ht->nNextFreeElement = __fill_idx; \
 		__fill_ht->nInternalPointer = 0; \
 	} while (0)
 

I will send a PR soon, could you please try this patch?
 [2015-03-03 10:18 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=70bd500645da48f3c2eb2ebbe916a2abe51a48e0
Log: Fixed #69166 (Assigning array_values() to array does not reset key counter)
 [2015-03-03 10:18 UTC] laruence@php.net
-Status: Open +Status: Closed
 [2016-07-20 11:39 UTC] davey@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=70bd500645da48f3c2eb2ebbe916a2abe51a48e0
Log: Fixed #69166 (Assigning array_values() to array does not reset key counter)
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 07 12:01:35 2025 UTC