Re: RFC - Array slice syntactic sugar

From: Date: Fri, 20 Mar 2015 14:41:02 +0000
Subject: Re: RFC - Array slice syntactic sugar
References: 1 2 3 4 5 6 7 8 9 10 11 12 13  Groups: php.internals 
Request: Send a blank email to internals+get-85295@lists.php.net to get a copy of this message
Alex Bowers wrote on 20/03/2015 13:40:
Still not sure how we can implement a range of strings. But since thats for a different feature, I'll leave that issue for now.
I can't resist a quick answer: if you can define a key-based slice at all, you can define it for both integer and string keys. Bear in mind the a:b here isn't a range, it's just a pair of values specifying which items to include in the slice. If the definition of a key-based slice is "all elements whose key satisfies $key >= $a && $key <= $b", then you're just doing comparisons between strings, which are defined as lexical order. So $dictionary['elephant':'snake'] returns all elements with keys which sort lexically between 'elephant' and 'snake', regardless of whether the array is sorted. Alternatively, a key-based slice could look up the position in the array of the two keys, and then perform a positional slice between those positions, i.e. $array[$a:$b] == $array[ @ key_position($array, $a) : key_position($array, $b) ]. I'm not sure that's particularly intuitive or useful, but again, it has no problem with string keys. Regards, -- Rowan Collins [IMSoP]

« previous php.internals (#85295) next »