Req #72083 [NEW]: native array_add function; get last added key instead of count(array) with push
From: raat1979 at gmail dot com
Operating system: Irrelevant
PHP version: Next Minor Version
Package: SPL related
Bug Type: Feature/Change Request
Bug description:native array_add function; get last added key instead of count(array) with push
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 bug report at https://bugs.php.net/bug.php?id=72083&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=72083&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=72083&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=72083&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=72083&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=72083&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=72083&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=72083&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=72083&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=72083&r=support
Expected behavior: https://bugs.php.net/fix.php?id=72083&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=72083&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=72083&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=72083&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=72083&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=72083&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=72083&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=72083&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=72083&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=72083&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=72083&r=mysqlcfg
Thread (8 messages)
- raat1979 at gmail dot com