note 92682 added to function.array-pop
| From: | sneskid at hotmail dot com | Date: | Tue, 04 Aug 2009 02:22:21 +0000 |
| Subject: | note 92682 added to function.array-pop | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-159069@lists.php.net to get a copy of this message | ||
As already mentioned, array_pop always returns a copy, never a reference, which can be a problem in
some cases.
Here is a pop function that allows you to get the reference.
(keep in mind it will alter the internal pointer of the array)
<?php
function &array_rpop(&$a){
end($a);
$k=key($a);
$v=&$a[$k];
unset($a[$k]);
return $v;
}
// try this:
$A='A';
$B=array(&$A);
//$C=&array_rpop($B); // will work
$C=&array_pop($B); // won't work
$C='C';
var_dump($A,$B,$C);
?>
I hope the PHP people will make the regular function work with references.
----
Server IP: 64.71.164.2
Probable Submitter: 68.146.80.45
----
Manual Page -- http://www.php.net/manual/en/function.array-pop.php
Edit -- https://master.php.net/note/edit/92682
Del: integrated -- https://master.php.net/note/delete/92682/integrated
Del: useless -- https://master.php.net/note/delete/92682/useless
Del: bad code -- https://master.php.net/note/delete/92682/bad+code
Del: spam -- https://master.php.net/note/delete/92682/spam
Del: non-english -- https://master.php.net/note/delete/92682/non-english
Del: in docs -- https://master.php.net/note/delete/92682/in+docs
Del: other reasons-- https://master.php.net/note/delete/92682
Reject -- https://master.php.net/note/reject/92682
Search -- https://master.php.net/manage/user-notes.php