Re: Support negative indexes for arrays and strings

From: Date: Sun, 17 Jun 2012 22:52:54 +0000
Subject: Re: Support negative indexes for arrays and strings
References: 1 2 3 4 5 6 7 8  Groups: php.internals 
Request: Send a blank email to internals+get-60874@lists.php.net to get a copy of this message
Hi Marc, Am 17.06.2012 um 21:53 schrieb Marc Easen: [...] > Numerical keyed array: > > $a = array('foo', 'bar', 'baz'); > $a[0] === 'foo' > > I would expect: > > $a[-1] === 'baz'; > > An string keyed array: > > $b = array('foo' => 1, 'bar' => 2, 'baz' => 3); > $b[0] === 1; > > Would generate an E_NOTICE: PHP Notice: Undefined offset: 0 in php shell code on line 1. > An negative offset would also generate the same E_NOTICE. > > So going back to your point, the change would only be to numeric based arrays (list) and > strings. It could be possible to string keyed arrays to be accessed by the numeric counter parts, > but I'm not familiar to comment on if this is even possible. I see, must have overread that. This makes it slightly better but not optimal, as too varying behavior for hashes vs. lists will be harder to explain. We could go down that road and separate the both more and more but given the history (and the usage patterns) of arrays in PHP I'm not convinced this is a good idea. I would prefer something like this (and no, I don't propose using ':' as an operator for real): $string = "bar"; $string:-1 // "b" $string:-2 // "r" $string:-10 // NULL $list = array("one", "two", "three"); $list:-1 // "three" $list:-2 // "two" $list:-10 // NULL $hash = array("one" => 1, "two" => 2, "three" => 3) $hash:-1 // 3 $hash:-2 // 2 $hash:-10 // 10 As using arrays as hashes let’s them behave in a FIFO kind of way, we can do proper slicing. > It seems this topic has generated a lot of interest, I feel the best way to proceed would be to > write a RFC. I've never written one before so could someone give me access to create a page on > the RFC site or create a RFC for me and give me permission to update it, my username is > 'easen'. For sure, RFC makes sense. I like the general idea, I’m just not sure about the syntax. cu, Lars

Attachment: [application/pgp-signature] Message signed with OpenPGP using GPGMail signature.asc
« previous php.internals (#60874) next »