Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62623 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 11123 invoked from network); 1 Sep 2012 09:17:45 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Sep 2012 09:17:45 -0000 Authentication-Results: pb1.pair.com smtp.mail=theanomaly.is@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=theanomaly.is@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.42 as permitted sender) X-PHP-List-Original-Sender: theanomaly.is@gmail.com X-Host-Fingerprint: 209.85.215.42 mail-lpp01m010-f42.google.com Received: from [209.85.215.42] ([209.85.215.42:34346] helo=mail-lpp01m010-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id CC/21-03671-8B2D1405 for ; Sat, 01 Sep 2012 05:17:44 -0400 Received: by lahl5 with SMTP id l5so2783619lah.29 for ; Sat, 01 Sep 2012 02:17:40 -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=GxmLEqUhYYkAHUOzATzKd68Dc6Vf0lFcqvxvLKJ+dxs=; b=YM3D8PBkJSqvg/cJqjwmKJMiqgUshkJd4GAb9pwbvW0UN28HO/DAgaccuZfJzZ4sow 9spPKwTG64IMNS9ASp249iKmnXapwa6k0GZN1xHmPo4dLcHtH4A4ZmpN6YBlQ0x786ON Tdq3zLr4qbOdDik2TVFwvRLaPvNqtvHmLPPDvbSA0dW0Bwxvr9jWxIsR8FP7t0pKyNcf m535zHVVg2jrWh69EWgNHqkdYIX/gRBz5BcZUvBhDypZxeCTIXyyzrkXWl8cjwvSoQMT r5BZ81vmxdQvC3CO3UxVkmcevqKxlnaVz//eW48GbykyDdP8sHEEDNXgROTkj90R71nR 2JwQ== MIME-Version: 1.0 Received: by 10.112.88.2 with SMTP id bc2mr1648800lbb.61.1346491060592; Sat, 01 Sep 2012 02:17:40 -0700 (PDT) Received: by 10.112.8.7 with HTTP; Sat, 1 Sep 2012 02:17:40 -0700 (PDT) In-Reply-To: <5041CCE7.1000309@lsces.co.uk> References: <5041CCE7.1000309@lsces.co.uk> Date: Sat, 1 Sep 2012 05:17:40 -0400 Message-ID: To: Lester Caine Cc: "internals@lists.php.net" Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [PHP-DEV] Support negative indexes for arrays and strings From: theanomaly.is@gmail.com (Sherif Ramadan) On Sat, Sep 1, 2012 at 4:52 AM, Lester Caine wrote: > Sherif Ramadan wrote: >> >> As it stands today, that code would result in the following: >> >> $numbers = array(); >> $numbers[-1] = 5; >> $numbers[] = 6; >> var_dump($numbers); >> /* >> array(2) { >> [-1]=> >> int(5) >> [0]=> >> int(6) >> } >> */ > > > I think that it is just clarifying this further by adding > $numbers = array(0,1,2,3,4); > Which gives > array(7) { [0]=> int(0) [1]=> int(1) [2]=> int(2) [3]=> int(3) [4]=> int(4) > [-1]=> int(5) [5]=> int(6) } I have no idea how it would. I think you supplied the wrong code here. > and > foreach( $numbers as $value ) { print($value); } gives 0123456 as I would > have expected! > AND print( $numbers[-1] ); still returns '5' > > I can NOW use ksort() to really move the '5' to the top of the list. > array(7) { [-1]=> int(5) [0]=> int(0) [1]=> int(1) [2]=> int(2) [3]=> int(3) > [4]=> int(4) [5]=> int(6) } > > Bottom line ... perhaps we need a DIFFERENT means of identifying position in > the array since using -ve keys IS a valid situation in real life. Personally > I use -ve positions in the database tables to save having to process all > records when adding items at the top, so the associative array quite > correctly has -ve key values. I would not want PHP screwing around with that > when reading the array in :) I can't understand what you mean by "Different means of identifying position in the array"? If you mean a way to access an array's element by its position in the array then yes, we already have that. It's called array_slice() see http://php.net/array-slice which allows you to access elements in the array by their offset and that includes using a negative offset. The key and the offset are two completely different things. > > I think I am right in saying that 'real array' always start at 0 or 1, and > positioning is explicit. So for simple stings then ne6ative indexes make > perfect sense. But for PHP 'container arrays' we can legally have -ve keys, > and removing that is simply not acceptable. > Yes, the position is always explicit, the key isn't ever responsible for determining the position of an element in the array. The value of the PHP key has never been responsible for determining the position of the element in the array. $array[99999] = 'first element'; $array[0] = 'second element'; var_dump($array); /* array(2) { [99999]=> string(13) "first element" [0]=> string(14) "second element" } */ $array[0] = 'first element'; $array[99999] = 'second element'; var_dump($array); /* array(2) { [99999]=> string(14) "second element" [0]=> string(13) "first element" } */ The order has always been determined internally so the values of these keys mean nothing in terms of order as you can see. > -- > Lester Caine - G8HFL > ----------------------------- > Contact - http://lsces.co.uk/wiki/?page=contact > L.S.Caine Electronic Services - http://lsces.co.uk > EnquirySolve - http://enquirysolve.com/ > Model Engineers Digital Workshop - http://medw.co.uk > Rainbow Digital Media - http://rainbowdigitalmedia.co.uk > > > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php >