|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #72083 native array_add function; get last added key instead of count(array) with push
Submitted: 2016-04-23 03:08 UTC Modified: 2017-09-11 12:17 UTC
From: raat1979 at gmail dot com Assigned:
Status: Open Package: Arrays related
PHP Version: Next Minor Version OS: Irrelevant
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2016-04-23 03:08 UTC] raat1979 at gmail dot com
array_push returns count(array) as a return value
For arrays containing associative key this is quite useless
it would have been better if it returned the key of the last item it added

Test script:
//php implementation
function array_add(array &$array,$value /*[, $...]*/){
	$values = func_get_args();      //get all values
	$values[0]= &$array;            //BY REFERENCE!
	$org=key($array);               //where are we?
	end($array);                    // move to the last item
	$key = key($array);             //get the key of the last item
		//was at eof, added something, moved to it,fine
		return $key;
	}elseif($org<(count($array)/2)){ //somewhere in the middle +/- is fine
		while (key($array) !== $org) next($List);
		while (key($array) !== $org) prev($List);
	return $key;

Expected result:
$pr = array(
    [foo] => bar
    [bar] => foo

array_push($pr,1)   //returns 3

array_push($pr,1,2) //returns 4

 array_add($pr,1)   //returns 0

 array_add($pr,1,2) //returns 1


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2016-04-23 06:53 UTC] raat1979 at gmail dot com
looking at the php source array_push ends with zend_hash_num_elements
I guess that's basically performing the equivalent of a php count(array)

shows there is a call zend_hash_next_free_element

the add function would need to work exactly like the push function except for the last element which should call zend_hash_next_free_element just before storing the value and store its result in a variable to be used as the return value
(or break on first failure and issue error like push does)
 [2016-04-23 07:37 UTC] raat1979 at gmail dot com
Thinking about this there is currently is no (normal) way to get the next available numeric member of an array, one could consider this a bug
 [2017-09-10 21:23 UTC]
-Status: Open +Status: Feedback -Package: SPL related +Package: Arrays related -Assigned To: +Assigned To: cmb
 [2017-09-10 21:23 UTC]
Sorry, I fail to understand what you are proposing. If I want to
add an element to an arbitrary array, I would use

  $pr[] = $element;

Wouldn't that work for you?
 [2017-09-10 21:40 UTC] raat1979 at gmail dot com
-Status: Feedback +Status: Assigned
 [2017-09-10 21:40 UTC] raat1979 at gmail dot com
Sure, but after this I'd like to know the index of the item added. (which would contain a mix of numeric and assoc keys)
 [2017-09-10 21:44 UTC] raat1979 at gmail dot com
Basically we need a php implementation of zend_hash_next_free_element
 [2017-09-11 12:17 UTC]
-Status: Assigned +Status: Open -Assigned To: cmb +Assigned To:
 [2017-09-11 12:17 UTC]
> Basically we need a php implementation of
> zend_hash_next_free_element

Thanks for the clarification. A pull request would be welcome.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Mon Sep 20 00:03:37 2021 UTC