# substr/array_slice in []

- Thread: https://externals.io/message/32513
- Original archive: https://news-web.php.net/php.internals/32513
- Started by Alexey Zakhlestin on 2007-10-01T11:30:49+00:00
- Messages: 30
- Page 1 of 2: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/32513.md?page=2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #32513 — Alexey Zakhlestin — 2007-10-01T11:30:49+00:00

URL: https://externals.io/message/32513#32513

Here http://www.php.net/~derick/meeting-notes.html#cleanup-for-vs is a
phrase "For both strings and arrays, the [] operator will support
 `substr()`/array_slice() functionality"

will it appear in php5.x (php 5.3)?


-- 
Alexey Zakhlestin
http://blog.milkfarmsoft.com/

---

## #32514 — Ilia Alshanetsky — 2007-10-01T12:32:29+00:00

URL: https://externals.io/message/32513#32514 · In reply to: #32513

This was not on the table and the time of the 5.3 discussion, I for  
one think its a bit too much magic.



> Here http://www.php.net/~derick/meeting-notes.html#cleanup-for-vs is a
> phrase "For both strings and arrays, the [] operator will support
> `substr()`/array_slice() functionality"
>
> will it appear in php5.x (php 5.3)?
>
>
> -- 
> Alexey Zakhlestin
> http://blog.milkfarmsoft.com/
>
> -- 
> 
> 
>

Ilia Alshanetsky

---

## #32515 — Antony Dovgal — 2007-10-01T12:46:29+00:00

URL: https://externals.io/message/32513#32515 · In reply to: #32514

> This was not on the table and the time of the 5.3 discussion, I for  
> one think its a bit too much magic.

Yeah, too Perl-ish for me.

-- 
Wbr, 
Antony Dovgal

---

## #32517 — Stanislav Malyshev — 2007-10-01T16:32:17+00:00

URL: https://externals.io/message/32513#32517 · In reply to: #32513

Alexey Zakhlestin wrote:
> Here http://www.php.net/~derick/meeting-notes.html#cleanup-for-vs is a
> phrase "For both strings and arrays, the [] operator will support
> `substr()`/array_slice() functionality"
> 
> will it appear in php5.x (php 5.3)?

I think since we do have substr/array_slice there's no need to overload 
[] operator.
-- 
Stanislav Malyshev, Zend Software Architect
stas@zend.com   http://www.zend.com/
(408)253-8829   MSN: stas@zend.com

---

## #32519 — Andrei Zmievski — 2007-10-01T18:01:22+00:00

URL: https://externals.io/message/32513#32519 · In reply to: #32513

It's on my TODO list.

-Andrei

Alexey Zakhlestin wrote:
> Here http://www.php.net/~derick/meeting-notes.html#cleanup-for-vs is a
> phrase "For both strings and arrays, the [] operator will support
> `substr()`/array_slice() functionality"
> 
> will it appear in php5.x (php 5.3)?

---

## #32521 — Martin Alterisio — 2007-10-01T19:36:31+00:00

URL: https://externals.io/message/32513#32521 · In reply to: #32513

Sorry to bother, I have a few questions on this matter.
How will this impact on the SPL ArrayAccess and related interfaces and
objects?
Will there be an interface to this functionality?
If so, how will ranges be passed through to this interface?
Will this be consistent with `substr()` and `array_slice()` if used with an
ArrayAccess implementation?

Thank you and sorry for the bother.

Regards,

Martin Alterisio

2007/10/1, Alexey Zakhlestin <indeyets@gmail.com>:
>
> Here http://www.php.net/~derick/meeting-notes.html#cleanup-for-vs is a
> phrase "For both strings and arrays, the [] operator will support
> `substr()`/array_slice() functionality"
>
> will it appear in php5.x (php 5.3)?
>
>
> --
> Alexey Zakhlestin
> http://blog.milkfarmsoft.com/

---

## #32522 — Stanislav Malyshev — 2007-10-01T19:41:49+00:00

URL: https://externals.io/message/32513#32522 · In reply to: #32521

> Sorry to bother, I have a few questions on this matter.
> How will this impact on the SPL ArrayAccess and related interfaces and
> objects?

That's the problem with such syntax - one would expect it works with 
ArrayAccess and it starts to be complicated...

-- 
Stanislav Malyshev, Zend Software Architect
stas@zend.com   http://www.zend.com/
(408)253-8829   MSN: stas@zend.com

---

## #32542 — Alexey Zakhlestin — 2007-10-02T12:35:43+00:00

URL: https://externals.io/message/32513#32542 · In reply to: #32521

> Sorry to bother, I have a few questions on this matter.
> How will this impact on the SPL ArrayAccess and related interfaces and
> objects?
> Will there be an interface to this functionality?
> If so, how will ranges be passed through to this interface?
> Will this be consistent with `substr()` and `array_slice()` if used with an
> ArrayAccess implementation?

I guess it can be made to work with current ArrayAccess, but result
will be quite slow. (it will need to query requested elements
one-by-one and recombine those in array)

But adding another interface can solve the problem. Ranges can be
passed exactly the way they are passed to [] operator

public function rangeGet($start, $length);
public function rangeSet($start, $length, array $data);


-- 
Alexey Zakhlestin
http://blog.milkfarmsoft.com/

---

## #32553 — Martin Alterisio — 2007-10-02T15:37:20+00:00

URL: https://externals.io/message/32513#32553 · In reply to: #32542

2007/10/2, Alexey Zakhlestin <indeyets@gmail.com>:
>
> 
> > […]
>
> I guess it can be made to work with current ArrayAccess, but result
> will be quite slow. (it will need to query requested elements
> one-by-one and recombine those in array)
>
> But adding another interface can solve the problem. Ranges can be
> passed exactly the way they are passed to [] operator
>
> public function rangeGet($start, $length);
> public function rangeSet($start, $length, array $data);
>
>
> --
> Alexey Zakhlestin
> http://blog.milkfarmsoft.com/
>

In that case rangeSet shouldn't have the third argument type hinted. The
supplied data could be an ArrayAccess implementor.

---

## #32564 — Jeff Griffiths — 2007-10-02T17:48:04+00:00

URL: https://externals.io/message/32513#32564 · In reply to: #32515

Antony Dovgal wrote:
> 
> > […]
> 
> Yeah, too Perl-ish for me.

Actually, it's a lot like Python list slicing, which is a great feature 
and a clear enough syntax.

cheers, Jeff

---

## #32586 — Cristian Rodriguez — 2007-10-03T08:31:51+00:00

URL: https://externals.io/message/32513#32586 · In reply to: #32515

2007/10/1, Antony Dovgal <tony@daylessday.org>:

> Yeah, too Perl-ish for me.


=)  I dont see the need of implementing this either.



-- 
http://www.kissofjudas.net/

---

## #32637 — Larry Garfield — 2007-10-05T01:05:30+00:00

URL: https://externals.io/message/32513#32637 · In reply to: #32542

> 
> > […]
>
> I guess it can be made to work with current ArrayAccess, but result
> will be quite slow. (it will need to query requested elements
> one-by-one and recombine those in array)
>
> But adding another interface can solve the problem. Ranges can be
> passed exactly the way they are passed to [] operator
>
> public function rangeGet($start, $length);
> public function rangeSet($start, $length, array $data);

Here's the question I see.  Right now, does an ArrayAccess object work with 
 `array_slice()`?  If so, then [2, 5] syntax would be just some nice syntactic 
sugar.  If not, then it becomes a powerful new feature, and implementing it 
on normal arrays and strings becomes just a matter of consistent syntax.

Personaly I kinda like it, but I know I'm not the one coding it. :-)

-- 
Larry Garfield			AIM: LOLG42
larry@garfieldtech.com		ICQ: 6817012

"If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it."  -- Thomas 
Jefferson

---

## #32641 — Alexey Zakhlestin — 2007-10-05T05:20:45+00:00

URL: https://externals.io/message/32513#32641 · In reply to: #32637

> Here's the question I see.  Right now, does an ArrayAccess object work with
> `array_slice()`?  If so, then [2, 5] syntax would be just some nice syntactic
> sugar.  If not, then it becomes a powerful new feature, and implementing it
> on normal arrays and strings becomes just a matter of consistent syntax.

currently, array_slice doesn't work with ArrayAccess


-- 
Alexey Zakhlestin
http://blog.milkfarmsoft.com/

---

## #32645 — Derick Rethans — 2007-10-05T07:52:13+00:00

URL: https://externals.io/message/32513#32645 · In reply to: #32637

> 
> > […]
> 
> Here's the question I see.  Right now, does an ArrayAccess object work with 
> `array_slice()`?  If so, then [2, 5] syntax would be just some nice syntactic 
> sugar.

It doesn't matter if it works with `array_slice()` or not. [x] works with 
ArrayAccess, so you'd expect [x,y] to work as well otherwise it's 
inconsistent.

Derick

-- 
Derick Rethans
http://derickrethans.nl | http://ez.no | http://xdebug.org

---

## #32676 — Marcus Boerger — 2007-10-06T18:09:38+00:00

URL: https://externals.io/message/32513#32676 · In reply to: #32637

Hello Larry,

  ArrayAccess is not designed to work in any array functions and we
explicitly decided against going that route. We wanted ArrayAccess to
support the array syntax, so [x,y] should be supported if we want that.
However I agree that this kind of slicing is a bit too perlish or
pythonish.

marcus

Friday, October 5, 2007, 3:05:30 AM, you wrote:

> 
> > […]

> Here's the question I see.  Right now, does an ArrayAccess object work with 
> `array_slice()`?  If so, then [2, 5] syntax would be just some nice syntactic 
> sugar.  If not, then it becomes a powerful new feature, and implementing it 
> on normal arrays and strings becomes just a matter of consistent syntax.

> Personaly I kinda like it, but I know I'm not the one coding it.  

> -- 
> Larry Garfield                  AIM: LOLG42
> larry@garfieldtech.com          ICQ: 6817012

> "If nature has made any one thing less susceptible than all others of 
> exclusive property, it is the action of the thinking power called an idea, 
> which an individual may exclusively possess as long as he keeps it to 
> himself; but the moment it is divulged, it forces itself into the possession
> of every one, and the receiver cannot dispossess himself of it."  -- Thomas 
> Jefferson




Best regards,
 Marcus

---

## #32680 — Larry Garfield — 2007-10-06T18:38:47+00:00

URL: https://externals.io/message/32513#32680 · In reply to: #32676

I have no love for Perl (and a rather strong dislike of it), but I have to 
agree with those who say that "looks like Perl" is a lame reason to reject 
something.  PHP's object system looks an awful lot like Java, too.  That 
doesn't make it bad.  

"Too unreadable", "not approachable enough", "too inflexible", etc. are 
perfectly valid reasons to reject a syntax.  "Looks like <insert language 
here>" is not, I content, one of them.


> Hello Larry,
>
>   ArrayAccess is not designed to work in any array functions and we
> explicitly decided against going that route. We wanted ArrayAccess to
> support the array syntax, so [x,y] should be supported if we want that.
> However I agree that this kind of slicing is a bit too perlish or
> pythonish.
>
> marcus
>
> Friday, October 5, 2007, 3:05:30 AM, you wrote:
> > […]
>
> Best regards,
>  Marcus


-- 
Larry Garfield			AIM: LOLG42
larry@garfieldtech.com		ICQ: 6817012

"If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it."  -- Thomas 
Jefferson

---

## #32681 — Antony Dovgal — 2007-10-06T18:49:14+00:00

URL: https://externals.io/message/32513#32681 · In reply to: #32680

> I have no love for Perl (and a rather strong dislike of it), but I have to 
> agree with those who say that "looks like Perl" is a lame reason to reject 
> something.  PHP's object system looks an awful lot like Java, too.  That 
> doesn't make it bad.  
> 
> "Too unreadable", "not approachable enough", "too inflexible", etc. are 
> perfectly valid reasons to reject a syntax.  "Looks like <insert language 
> here>" is not, I content, one of them.

I believe it should be pretty clear that "too perlish" means "too cryptic 
and makes no sense because it duplicates already implemented functionality
(more than one way to do it, yeah)".

But "too perlish" is much shorter.

-- 
Wbr, 
Antony Dovgal

---

## #32682 — Alexey Zakhlestin — 2007-10-06T19:57:18+00:00

URL: https://externals.io/message/32513#32682 · In reply to: #32681

I hardly find it too cryptic (it makes some array algorythms much more
readable, actually)

and in this case, having such construction would allow us to implement
 `array_slice()` in userland (which is not true other way round) ;)


> 
> > […]
>
> I believe it should be pretty clear that "too perlish" means "too cryptic
> and makes no sense because it duplicates already implemented functionality
> (more than one way to do it, yeah)".
>
> But "too perlish" is much shorter.
>
> --
> Wbr,
> Antony Dovgal
>
> --
> 
> 
>
>


-- 
Alexey Zakhlestin
http://blog.milkfarmsoft.com/

---

## #32683 — Antony Dovgal — 2007-10-06T20:17:10+00:00

URL: https://externals.io/message/32513#32683 · In reply to: #32682

> I hardly find it too cryptic (it makes some array algorythms much more
> readable, actually)

Good for you.
 
> and in this case, having such construction would allow us to implement
> `array_slice()` in userland (which is not true other way round) ;)

I don't get your problem, sorry.

-- 
Wbr, 
Antony Dovgal

---

## #32691 — Emil Hernvall — 2007-10-07T23:39:51+00:00

URL: https://externals.io/message/32513#32691 · In reply to: #32683

On a related note. Code like:

$foo = array("a" => range(1,10), "b" => range(11,20));
print_r($foo[][0]);

results in

Fatal error: Cannot use [] for reading in Command line code on line 3

It would be incredibly useful when handling regular two-dimensional
database-resultsets. Essentially, what I'm after is things like this:

$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
$result = array_combine(result[]['my_id'], $result);

If I prepared a patch that made this possible, would it be considered
for inclusion? Is there any issues I've overlooked or other
objections?

---

## #32692 — Rasmus Lerdorf — 2007-10-07T23:44:29+00:00

URL: https://externals.io/message/32513#32692 · In reply to: #32691

Emil Hernvall wrote:
> On a related note. Code like:
> 
> $foo = array("a" => range(1,10), "b" => range(11,20));
> print_r($foo[][0]);
> 
> results in
> 
> Fatal error: Cannot use [] for reading in Command line code on line 3
> 
> It would be incredibly useful when handling regular two-dimensional
> database-resultsets. Essentially, what I'm after is things like this:
> 
> $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
> $result = array_combine(result[]['my_id'], $result);
> 
> If I prepared a patch that made this possible, would it be considered
> for inclusion? Is there any issues I've overlooked or other
> objections?

Yes, I object on the basis that I have no idea what that syntax should
do by looking at it.  The auto-incrementing [] array syntax has always
been a write-only construct.  Auto-incrementing on a read doesn't make
any sense which means you are overloading that syntax to mean something
completely different.

-Rasmus

---

## #32696 — Antony Dovgal — 2007-10-08T05:37:53+00:00

URL: https://externals.io/message/32513#32696 · In reply to: #32692

> > […]
> 
> Yes, I object on the basis that I have no idea what that syntax should
> do by looking at it.  The auto-incrementing [] array syntax has always
> been a write-only construct.  Auto-incrementing on a read doesn't make
> any sense which means you are overloading that syntax to mean something
> completely different.

This syntax makes no sense in write context to me either.
-1

-- 
Wbr, 
Antony Dovgal

---

## #32706 — Andrei Zmievski — 2007-10-09T02:25:50+00:00

URL: https://externals.io/message/32513#32706 · In reply to: #32676

So what? Just because other languages implemented a good idea first  
does not mean that it is verboten to us. PHP is result of years of  
evolution (and yes, Terry, it's also a ball of nails) and stealing  
ideas is one way to inject fresh genes into it.

-Andrei



> However I agree that this kind of slicing is a bit too perlish or
> pythonish.

---

## #32707 — Andrei Zmievski — 2007-10-09T02:32:42+00:00

URL: https://externals.io/message/32513#32707 · In reply to: #32681

I will use an example:

$foo = substr($bar, 0, 5) . "-" . substr($bar, 5);

or

$foo = $bar[:5] . "-" . $bar[5:];

I would argue that the second line is hardly more cryptic than the  
first one. And if we were concerned that concerned about duplicate  
functionality, we probably wouldn't have SimpleXML and similar things.

-Andrei
http://10fathoms.org/vu - daily photoblog


> I believe it should be pretty clear that "too perlish" means "too  
> cryptic
> and makes no sense because it duplicates already implemented  
> functionality
> (more than one way to do it, yeah)".
>
> But "too perlish" is much shorter.
>
> -- 
> Wbr,
> Antony Dovgal

---

## #32708 — Antony Dovgal — 2007-10-09T05:33:26+00:00

URL: https://externals.io/message/32513#32708 · In reply to: #32707

> I will use an example:
> 
> $foo = substr($bar, 0, 5) . "-" . substr($bar, 5);
> 
> or
> 
> $foo = $bar[:5] . "-" . $bar[5:];
>
> I would argue that the second line is hardly more cryptic than the  
> first one. 

How come?
It looks like you're reading $bar[':5'], but forgot the quotes.
On the other side, what could be easier than a function call?

> And if we were concerned that concerned about duplicate  
> functionality, we probably wouldn't have SimpleXML and similar things.

Mistakes done in the past do not mean we should continue to do them in the future.

-- 
Wbr, 
Antony Dovgal

---

Next page: https://externals.io/message/32513.md?page=2
