The latest comments in this thread are talking about having a symbol before
the range to show that it is by positional index. Current propositions for
this are ^ and *.

I'm not sure how such operation would be useful


Anywhere on the front-end where a foreach() is used, and expects at most
say, 10 items. But the full dataset is fetched back (to show a summary
after the first 10 or whatever other reason).

The old code would have required a counter, the new code does not. This
would make it cleaner for the front-end developer to understand.

<?php
$results = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];

// Old
$count = 0;

foreach($results as $result){
if($count++ > 9) break;
echo $result . "\n"; // 1 2 3 4 5 6 7 8 9
}

// New

foreach($results[*:9] as $result) {
echo $result . "\n"; // 1 2 3 4 5 6 7 8 9
}

---

$string = "abcdefghijklmnop";

// Old
echo substr($string, 0, 5); // abcde

// New
echo $string[*:4]; // abcdef

This is just a few basic examples, but should show that there is a use case
for it.

On 20 March 2015 at 20:52, Stanislav Malyshev <smalys...@gmail.com> wrote:

> Hi!
>
> >> My proposal is something similar to Pythons slice, in PHP this would
> look
> >> like:
> >>
> >> $slided = $array[1:4]
> >>
> >> This will get the elements in positions 1,2,3,4. (1 through 4
> inclusive),
> >> ignoring the actual key of the array. The result for an array will be an
> >> array with the keys preserved, in the same order.
>
> I'm not sure how such operation would be useful, and it definitely would
> not be intuitive, as $array[0] and $array[0:1] (assuming non-inclusive
> semantic, or [0:0] with inclusive semantics) would return completely
> different things. That would happen even if the array has only numeric
> keys! This is the main problem with this syntax - unlike most languages
> where it is used, PHP arrays are not vectors, they are ordered hashmaps.
> Sometimes this is very convenient, sometimes - like for this syntax - it
> is not. I think this is the major reason why such proposals failed in
> the past.
>
> --
> Stas Malyshev
> smalys...@gmail.com
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Reply via email to