Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:61227 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 70745 invoked from network); 14 Jul 2012 16:10:27 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Jul 2012 16:10:27 -0000 Authentication-Results: pb1.pair.com smtp.mail=dragoonis@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=dragoonis@gmail.com; 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: dragoonis@gmail.com X-Host-Fingerprint: 209.85.161.170 mail-gg0-f170.google.com Received: from [209.85.161.170] ([209.85.161.170:44626] helo=mail-gg0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 93/6C-20866-2F991005 for ; Sat, 14 Jul 2012 12:10:27 -0400 Received: by ggnf2 with SMTP id f2so5126744ggn.29 for ; Sat, 14 Jul 2012 09:10:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=cPo0fVQ/saeoDcLgth2vZ3M9AXbGzw7uyl7iK/VjU24=; b=SPjS50Yla7O7WfElrMbV6cOybRcHWlY7SDL0SAdmKjbQzxwWzsFx1RX6/v+kq+uVdQ 6zYgPSNxn3lINoAvKM8+HvGNuPoCTk2glBfNYxMEPS42obCiCDPyLUAeo4T/2DvbPqVX jsY1Gs9lJ7BZjLG7/FyCvU8Z4MA0nLFZQFS8GgRINuGSR+SnzOd+OjygljLTG9GoZ8C0 sq/wOhn8tUhbbvJQ1z/ujn4mA5oqmpI6cGXJrYoQfAB/QFBtdpeJWa4jvuMqtM3MmqPv Frfi75kVA5/cn/jAaRwLlZBcTtFnxFaZd1AmKgzoelM2ywFOzGTS75f4eTJDo97m5KMg EDIA== MIME-Version: 1.0 Received: by 10.50.217.137 with SMTP id oy9mr1741293igc.56.1342282223260; Sat, 14 Jul 2012 09:10:23 -0700 (PDT) Received: by 10.64.32.72 with HTTP; Sat, 14 Jul 2012 09:10:23 -0700 (PDT) In-Reply-To: References: <5000BF44.3080907@sugarcrm.com> Date: Sat, 14 Jul 2012 17:10:23 +0100 Message-ID: To: John LeSueur Cc: PHP Internals List Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [PHP-DEV] Re: array_last_key() function From: dragoonis@gmail.com (Paul Dragoonis) (forgot to CC internals on the previous email) On Sat, Jul 14, 2012 at 4:42 PM, John LeSueur wrote: > Readability is solved by creating a userland function: > > function array_last_key(&$array) > { > return key(array_slice($array, -1,1,true)); > } Another userland function, with a by-ref parameter. Again more unnecessary complexity, solved by our new friend array_last_key. I hope this gets put into a branch soon, I'll happily +1 vote it. - Paul. > > $lastkey = array_last_key($array); > > Now, it may still be quicker, or use less memory(temporarily) if it were a > core function, but I'd like to see numbers before making a decision. I would > still want to see a significant advantage before it is added to core. > > > On Sat, Jul 14, 2012 at 9:33 AM, Paul Dragoonis wrote: >> >> $lastKey = key(array_slice($array,-1,1,true)); >> vs >> $lastKey = array_last_key($array); >> >> The latter will be quicker, use less memory and easier to read/maintain. >> >> - Paul. >> >> On Sat, Jul 14, 2012 at 4:28 PM, John LeSueur >> wrote: >> > >> > >> > On Sat, Jul 14, 2012 at 4:34 AM, Paul Dragoonis >> > wrote: >> >> >> >> On Sat, Jul 14, 2012 at 3:04 AM, Anthony Ferrara >> >> wrote: >> >> > Stas, >> >> > >> >> >> I like this idea. array_first_key would be nice too >> >> >> >> >> >> I am probably missing something, but what those would allow to do >> >> >> that >> >> >> rewind/end+key() doesn't do? >> >> >> >> >> > >> >> > The big thing that it does that end()+key() doesn't do is really what >> >> > it >> >> > doesn't do (update the internal pointer). >> >> > >> >> > end() modifies the array pointer. So if you try this in user-land: >> >> > >> >> > function array_last_key(array $array) { >> >> > end($array); >> >> > return key($array); >> >> > } >> >> > >> >> > It will always force a full copy of the array (at least the hash >> >> > table). >> >> > Implementing this as a core function however would eliminate that one >> >> > side-effect... >> >> >> >> and it beats doing $var = null; if(isset($array[0])) { $var = >> >> $array[0]; } >> >> >> > Not sure that does the trick(you're pulling values, not keys, and >> > assuming >> > 0-based array), but... >> > >> > What about reset(array_keys($array)); or end(array_keys($array)); which >> > only >> > makes copies of the keys? >> > or even better key(array_slice($array, 0, 1, true)); or >> > key(array_slice($array,-1,1,true)); which makes a copy of one element of >> > the >> > array? >> > >> > I'm of the opinion that we mostly have enough array_* functions, and new >> > ones have to provide a significant advantage to be accepted. If these >> > functions are significantly faster than the alternatives, then perhaps >> > they >> > have merit, but they seem like convenience functions, rather than >> > fundamentals. >> > >> > John > >