Re: Support negative indexes for arrays and strings

From: Date: Sat, 01 Sep 2012 08:52:55 +0000
Subject: Re: Support negative indexes for arrays and strings
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-62621@lists.php.net to get a copy of this message
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) } 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 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. -- 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

« previous php.internals (#62621) next »