Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:57859 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 64910 invoked from network); 14 Feb 2012 11:15:26 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Feb 2012 11:15:26 -0000 Received: from [127.0.0.1] ([127.0.0.1:22187]) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ECSTREAM id 5B/72-49308-E424A3F4 for ; Tue, 14 Feb 2012 06:15:26 -0500 Authentication-Results: pb1.pair.com header.from=mrpedro17@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=mrpedro17@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.161.170 as permitted sender) X-PHP-List-Original-Sender: mrpedro17@gmail.com X-Host-Fingerprint: 209.85.161.170 mail-gx0-f170.google.com Received: from [209.85.161.170] ([209.85.161.170:42685] helo=mail-gx0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 81/52-49308-1EE3A3F4 for ; Tue, 14 Feb 2012 06:00:49 -0500 Received: by ggki2 with SMTP id i2so3253905ggk.29 for ; Tue, 14 Feb 2012 03:00:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; bh=ArVnYfm/rUg4YL6B5S671pdBhvIiA/ZxcB1GzAJneBk=; b=fKXBxVZc/Dhnws2BVJze2D6+7nXmVgTNam1erQS8cv0VM4WsansZUD9evMkC1fJxNO SkfmiEcnlN2+P8gjxLXVdWfpQoowO0eYIPIWY0xIlND/jM/RPk/q++Go8cJQnNKlkmzx M/fmYKDuJN5S3RCt8TFi3VZaGsH19ZxH5xH8A= MIME-Version: 1.0 Received: by 10.236.127.145 with SMTP id d17mr25501622yhi.131.1329217246833; Tue, 14 Feb 2012 03:00:46 -0800 (PST) Received: by 10.236.156.42 with HTTP; Tue, 14 Feb 2012 03:00:46 -0800 (PST) Date: Tue, 14 Feb 2012 11:00:46 +0000 Message-ID: To: internals@lists.php.net Content-Type: multipart/alternative; boundary=20cf300faa25d3adaa04b8ea8139 Subject: New Array function - limited push From: mrpedro17@gmail.com (Pedro Pereira) --20cf300faa25d3adaa04b8ea8139 Content-Type: text/plain; charset=ISO-8859-1 Hi. I'm Pedro and I'm from Portugal. I've been working with php for a while but only now i decided to try my luck and propose a new array function that will become handy for many people :) This function adds new elements to the end of an array but limits the amount of total elements it can have, by eliminating the oldest entries. *Example:* Array limit: 4 1, 2, 3, 4 -> 4 elements When you try to add a fifth element: 2, 3, 4, 5 -> $ elements The good thing is that, even if you in the middle decide to limit the array to 10, it will store 10 elements but if you decide to put an inferior array limit, it will remove the oldest entries until the limit size is achieved. Example: Limit 6 elements: 1, 2, 3, 4, 5, 6 -> 6 elements Limit 3 When you try to add a new element: 5, 6, 7 -> 3 elements The function is written bellow: function array_push_limited(&$arrayValues, $newElement, $limit){ $count = count($arrayValues); if($count == 0){ $arrayValues[0] = $newElement; } else if($count < $limit){ array_push($arrayValues, $newElement); } else if ($count == $limit) { array_shift($arrayValues); array_push($arrayValues, $newElement); } else if ($count > $limite) { do{ array_shift($arrayValues); } while(count($arrayValues) > $limit - 1); array_push($arrayValues, $newElement); } } I have some more proposals but in time, after I improve them, I will send an email with them. I hope you see an advantage into put this as an array function for php. *Regards,* *Pedro Pereira* --20cf300faa25d3adaa04b8ea8139--