Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:57860 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 68272 invoked from network); 14 Feb 2012 12:05:38 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Feb 2012 12:05:38 -0000 Authentication-Results: pb1.pair.com header.from=patrick.allaert@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=patrick.allaert@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.54 as permitted sender) X-PHP-List-Original-Sender: patrick.allaert@gmail.com X-Host-Fingerprint: 74.125.82.54 mail-ww0-f54.google.com Received: from [74.125.82.54] ([74.125.82.54:33530] helo=mail-ww0-f54.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 2B/00-02203-F0E4A3F4 for ; Tue, 14 Feb 2012 07:05:37 -0500 Received: by wgbdq12 with SMTP id dq12so5358842wgb.11 for ; Tue, 14 Feb 2012 04:05:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=OfP6WeObZIG3ic6ZcnKrNrQN8PfL04IJ3H0uUNyCjvI=; b=FV1V6kXCRruQPL2Cmby9qchlWrU5pD0wG/WCNNKgLypat+mMMnbYOzO4cK8Z8l4lhV FX78/UhIPuZQ3fFl4L2gNI7d9n/o7WhiFExYMuQGPxo6pP+S5tyJ2KhcEU+w4JwXRNeg BwUPDYS5zvZcDXIcuHzLZrSBdH4ateY5PBr9g= MIME-Version: 1.0 Received: by 10.180.7.231 with SMTP id m7mr3920267wia.3.1329221133078; Tue, 14 Feb 2012 04:05:33 -0800 (PST) Sender: patrick.allaert@gmail.com Received: by 10.223.96.204 with HTTP; Tue, 14 Feb 2012 04:05:33 -0800 (PST) In-Reply-To: References: Date: Tue, 14 Feb 2012 13:05:33 +0100 X-Google-Sender-Auth: nNqRUxB2CAfArFzC9PQIK0_z6jo Message-ID: To: Pedro Pereira Cc: internals@lists.php.net Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] New Array function - limited push From: patrickallaert@php.net (Patrick ALLAERT) 2012/2/14 Pedro Pereira : > 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 arr= ay > to 10, it will store 10 elements but if you decide to put an inferior arr= ay > 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 =3D count($arrayValues); > =C2=A0if($count =3D=3D 0){ > $arrayValues[0] =3D $newElement; > } > else if($count < $limit){ > array_push($arrayValues, $newElement); > } > else if ($count =3D=3D $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* Hi Pedro, I suggest you to use array_splice() to achieve this. There is no real benefit to implement such a new array function as this is probably way too specific to be able to push elements and always being able to provide an array dimension at every single function calls. If the size isn't supposed to change, then you might want to try defining a new Spl class to implement a circular buffer where the size would be specified at creation time. You might want to take some inspiration from the SplFixedArray class. Regards, Patrick