Req #72083 [Opn]: native array_add function; get last added key instead of count(array) with push

From: Date: Sat, 23 Apr 2016 06:53:34 +0000
Subject: Req #72083 [Opn]: native array_add function; get last added key instead of count(array) with push
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-200711@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=72083&edit=1 ID: 72083 User updated by: raat1979 at gmail dot com Reported by: raat1979 at gmail dot com Summary: native array_add function; get last added key instead of count(array) with push Status: Open Type: Feature/Change Request Package: SPL related Operating System: Irrelevant PHP Version: Next Minor Version Block user comment: N Private report: N New Comment: 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) http://www.phpinternalsbook.com/hashtables/hashtable_api.html 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) Previous Comments: ------------------------------------------------------------------------ [2016-04-23 03:08:34] raat1979 at gmail dot com Description: ------------ 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? call_user_func_array('array_push',$values); end($array); // move to the last item $key = key($array); //get the key of the last item if($org===null){ //was at eof, added something, moved to it,fine return $key; }elseif($org<(count($array)/2)){ //somewhere in the middle +/- is fine reset($array); while (key($array) !== $org) next($List); }else{ 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 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=72083&edit=1

« previous php.bugs (#200711) next »