note 81208 added to function.array-push
| From: | darkimmortal at dkimmortal dot com | Date: | Mon, 18 Feb 2008 19:31:12 +0000 |
| Subject: | note 81208 added to function.array-push | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-136249@lists.php.net to get a copy of this message | ||
I wrote this function for use with 'latest scores' on a quiz site, but it has many
potential uses.
<?php
/**
* @desc array_push and removes elements from the beginning of the array until it is within limit
* @param array Array to push on to
* @param mixed Passed to array push as 2nd parameter
* @param int Limit (default = 10)
*
* @return array New array
*/
function array_push_limit($array,$add,$limit=10){
array_push($array, $add);
do {
array_shift($array);
$size=count($array);
} while($size > $limit);
return $array;
}
?>
----------
EXAMPLE:
----------
<?php
$array=array(1, -5, 23, -66, 33, 54, 3);
print_r(array_push_limit($array, "HELLO", 4));
?>
----------
OUTPUT:
----------
Array
(
[0] => 33
[1] => 54
[2] => 3
[3] => HELLO
)
----
Server IP: 208.69.120.35
Probable Submitter: 172.213.198.214
----
Manual Page -- http://www.php.net/manual/en/function.array-push.php
Edit -- https://master.php.net/note/edit/81208
Del: integrated -- https://master.php.net/note/delete/81208/integrated
Del: useless -- https://master.php.net/note/delete/81208/useless
Del: bad code -- https://master.php.net/note/delete/81208/bad+code
Del: spam -- https://master.php.net/note/delete/81208/spam
Del: non-english -- https://master.php.net/note/delete/81208/non-english
Del: in docs -- https://master.php.net/note/delete/81208/in+docs
Del: other reasons-- https://master.php.net/note/delete/81208
Reject -- https://master.php.net/note/reject/81208
Search -- https://master.php.net/manage/user-notes.php