Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:61232 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 81323 invoked from network); 14 Jul 2012 18:10:27 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Jul 2012 18:10:27 -0000 Authentication-Results: pb1.pair.com smtp.mail=rasmus@lerdorf.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=rasmus@lerdorf.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain lerdorf.com from 209.85.160.42 cause and error) X-PHP-List-Original-Sender: rasmus@lerdorf.com X-Host-Fingerprint: 209.85.160.42 mail-pb0-f42.google.com Received: from [209.85.160.42] ([209.85.160.42:63772] helo=mail-pb0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id D6/4E-20866-316B1005 for ; Sat, 14 Jul 2012 14:10:27 -0400 Received: by pbbrp12 with SMTP id rp12so8327760pbb.29 for ; Sat, 14 Jul 2012 11:10:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:x-enigmail-version:content-type :content-transfer-encoding:x-gm-message-state; bh=Vn3pBKhxBZah+vpMvfHu0sOsDAvaKwMnDg6Wau1//5I=; b=YUS95rHQFSYIqIrQmm8ZPndSa9U3w+ZQ9rXIbJl+qGPjgoSb3l1aNymm2atcyZn4/m QpmfgHJ0x6OhoGTYNadBIimdfryv7ymdpHN2laiZlLN1Mto0sptY1SxciNDwE+IKR8Xo 9k3s5pPcaHc8X+B4DQ0j8eBp7QBfIvE+eC1ba6BMwhKPETUdEXjZxt/joQ/RiUnc7ztD ebgFAW0ZiLg8AWiZ9bof72wB7G1E/SSIADcklOW2yfUNGLdd3OGFINGEQfP/2oxYc44L R1lEz44N1iO01Or+xpUzItn8yWsDEE5/5IM3gZi0S2yW7JjLQtS1LCUGaVaGCEcWu8Zu 7L5w== Received: by 10.68.241.228 with SMTP id wl4mr13053148pbc.51.1342289424057; Sat, 14 Jul 2012 11:10:24 -0700 (PDT) Received: from [192.168.200.5] (c-50-131-44-225.hsd1.ca.comcast.net. [50.131.44.225]) by mx.google.com with ESMTPS id nu5sm5151244pbb.53.2012.07.14.11.10.21 (version=SSLv3 cipher=OTHER); Sat, 14 Jul 2012 11:10:23 -0700 (PDT) Message-ID: <5001B60C.60304@lerdorf.com> Date: Sat, 14 Jul 2012 11:10:20 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:13.0) Gecko/20120615 Thunderbird/13.0.1 MIME-Version: 1.0 To: Anthony Ferrara CC: Gustavo Lopes , Stas Malyshev , Brandon Wamboldt , Andrew Faulds , internals References: <5000BF44.3080907@sugarcrm.com> <5001AC64.7040304@lerdorf.com> In-Reply-To: <5001AC64.7040304@lerdorf.com> X-Enigmail-Version: 1.4.2 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Gm-Message-State: ALoCoQmhyb2q19vKR1cRd3mHPP41obgnSArkYX9jVHHYl2jIUGfeYocFonr9n3MqhA8kNRUhxCJh Subject: Re: [PHP-DEV] Re: array_last_key() function From: rasmus@lerdorf.com (Rasmus Lerdorf) On 07/14/2012 10:29 AM, Rasmus Lerdorf wrote: > On 07/14/2012 10:13 AM, Anthony Ferrara wrote: >> Gustavo, >> >> Why is the last key special? Why not a function to get the first or the >>> penultimate key? >>> >> >> How would such a function look? > > I think the function would look just like the function we have for doing > this. > > key(array_slice($arr,-2)); > > And no, this has no side-effect of changing the array pointer in $arr, > of course. > > And array_last_key() is identical to array_slice($arr,-1) is it not? > > The only thing array_last_key() saves is a single function call to > key(). That doesn't seem worth it to me. Ok, with the slight modification that for numeric keys we would want to preserve the keys. function array_last_key($arr) { return key(array_slice($arr,-1,null,true)); } And despite what the docs say, the array pointer is not changed. Test: $a = [ 'first'=>1, 'second'=>2, 'third'=>3, 'last'=>4 ]; next($a); next($a); echo key($a); echo array_last_key($a); echo key($a); This outputs: third last third -Rasmus