array_has_more

php.internals

(Marcus Börger)

23 years ago
Hello internals, during my todays work i was again reminded that still one array function is missing namely array_has_more. This function will return true if there are more elements in an array and false if not. The real problem is that next() doesn't work with false. A diff against PHP 5 is attached. If noone objects i'll commit this later on friday. Examle: <?php $ar = array(2,1,false,0); $i = 0; function show() { global $ar, $i; echo "loop: ";var_dump($i++); echo "curr: ";var_dump(current($ar)); echo "more: ";var_dump(array_has_more($ar)); echo "curr: ";var_dump(current($ar)); echo "next: ";var_dump(next($ar)); echo "\n"; } show(); show(); show(); show(); show(); show(); ?>
-- Best regards, Marcus mailto:helly@php.net

Andi Gutmans

23 years ago
You forgot to attach the diff :) Andi At 03:56 AM 4/7/2003 +0200, Marcus Börger wrote:

(Marcus Börger)

23 years ago
Hello Andi, damn, yeah, it was already to early in the morning when i sent the mail, i somehow didn't notice that i was trying to send a '.tx' file :-( Friday, July 4, 2003, 11:55:01 AM, you wrote: AG> You forgot to attach the diff :) AG> Andi AG> At 03:56 AM 4/7/2003 +0200, Marcus Börger wrote:
>>Hello internals, >> >> during my todays work i was again reminded that still one array function is >>missing namely array_has_more. This function will return true if there are >>more elements in an array and false if not. The real problem is that next() >>doesn't work with false. >> >>A diff against PHP 5 is attached. If noone objects i'll commit this later on >>friday. >> >>Examle: >><?php >> >>$ar = array(2,1,false,0); >>$i = 0; >> >>function show() { >> global $ar, $i; >> >> echo "loop: ";var_dump($i++); >> echo "curr: ";var_dump(current($ar)); >> echo "more: ";var_dump(array_has_more($ar)); >> echo "curr: ";var_dump(current($ar)); >> echo "next: ";var_dump(next($ar)); >> echo "\n"; >>} >> >>show(); >>show(); >>show(); >>show(); >>show(); >>show(); >> >>?> >> >>-- >>Best regards, >> Marcus mailto:helly@php.net >>-- >>PHP Internals - PHP Runtime Development Mailing List >>To unsubscribe, visit: http://www.php.net/unsub.php
-- Best regards, Marcus mailto:helly@php.net

Andi Gutmans

23 years ago
At 07:00 PM 4/7/2003 +0200, Marcus Börger wrote:
>Hello Andi, > >damn, yeah, it was already to early in the morning when i sent the mail, i >somehow didn't notice that i was trying to send a '.tx' file :-(
What you are saying is that differentiating between a null value from next() and not having more elements is impossible without such a function? (I just want to be sure I understand what your motivation is). I don't really have a problem with array_has_more() the only problem is the name is not consistent with the other methods but on the other hand we don't allow new functions which don't follow the naming conventions. Catch-22? :) Andi

(Marcus Börger)

23 years ago
Hello Andi, Friday, July 4, 2003, 10:18:41 PM, you wrote: AG> At 07:00 PM 4/7/2003 +0200, Marcus Börger wrote:
>>Hello Andi, >> >>damn, yeah, it was already to early in the morning when i sent the mail, i >>somehow didn't notice that i was trying to send a '.tx' file :-(
AG> What you are saying is that differentiating between a null value from AG> next() and not having more elements is impossible without such a function? AG> (I just want to be sure I understand what your motivation is). AG> I don't really have a problem with array_has_more() the only problem is the AG> name is not consistent with the other methods but on the other hand we AG> don't allow new functions which don't follow the naming conventions. AG> Catch-22? :) AG> Andi Yes and yes (but the problem is with false). Hm a new name, don't know. you you have a better idea than mine? First i thought about more but that itsn't really good. Then i went for has_more but new array functions should have prefix 'array_' so it became 'array_has_more'. marcus
>>Friday, July 4, 2003, 11:55:01 AM, you wrote: >> >>AG> You forgot to attach the diff :) >> >>AG> Andi >> >>AG> At 03:56 AM 4/7/2003 +0200, Marcus Börger wrote: >> >>Hello internals, >> >> >> >> during my todays work i was again reminded that still one array >> function is >> >>missing namely array_has_more. This function will return true if there are >> >>more elements in an array and false if not. The real problem is that next() >> >>doesn't work with false. >> >> >> >>A diff against PHP 5 is attached. If noone objects i'll commit this >> later on >> >>friday. >> >> >> >>Examle: >> >><?php >> >> >> >>$ar = array(2,1,false,0); >> >>$i = 0; >> >> >> >>function show() { >> >> global $ar, $i; >> >> >> >> echo "loop: ";var_dump($i++); >> >> echo "curr: ";var_dump(current($ar)); >> >> echo "more: ";var_dump(array_has_more($ar)); >> >> echo "curr: ";var_dump(current($ar)); >> >> echo "next: ";var_dump(next($ar)); >> >> echo "\n"; >> >>} >> >> >> >>show(); >> >>show(); >> >>show(); >> >>show(); >> >>show(); >> >>show(); >> >> >> >>?> >> >> >> >>-- >> >>Best regards, >> >> Marcus mailto:helly@php.net >> >>-- >> >>PHP Internals - PHP Runtime Development Mailing List >> >>To unsubscribe, visit: http://www.php.net/unsub.php >> >> >> >> >>-- >>Best regards, >> Marcus mailto:helly@php.net
-- Best regards, Marcus mailto:helly@php.net

Sascha Schumann

23 years ago
> What you are saying is that differentiating between a null value from > next() and not having more elements is impossible without such a function? > (I just want to be sure I understand what your motivation is). > I don't really have a problem with array_has_more() the only problem is the > name is not consistent with the other methods but on the other hand we > don't allow new functions which don't follow the naming conventions. > Catch-22? :)
The key() can never become false which is why array_has_more is not needed. - Sascha

(Marcus Börger)

23 years ago
Hello Sascha, Saturday, July 5, 2003, 6:34:26 AM, you wrote:
>> What you are saying is that differentiating between a null value from >> next() and not having more elements is impossible without such a function? >> (I just want to be sure I understand what your motivation is). >> I don't really have a problem with array_has_more() the only problem is the >> name is not consistent with the other methods but on the other hand we >> don't allow new functions which don't follow the naming conventions. >> Catch-22? :)
SS> The key() can never become false which is why array_has_more SS> is not needed. Yes and no. Yes key can't become false. No because array_has_more returns the information 'has more elements' before accessing them. That is different. It allows loops where key and element do not have to be cached.
-- Best regards, Marcus mailto:helly@php.net

Andi Gutmans

23 years ago
At 06:34 AM 5/7/2003 +0200, Sascha Schumann wrote:
> > What you are saying is that differentiating between a null value from > > next() and not having more elements is impossible without such a function? > > (I just want to be sure I understand what your motivation is). > > I don't really have a problem with array_has_more() the only problem is the > > name is not consistent with the other methods but on the other hand we > > don't allow new functions which don't follow the naming conventions. > > Catch-22? :) > > The key() can never become false which is why array_has_more > is not needed.
Why? You can use "" as a key in an array. Andi

Andi Gutmans

23 years ago
At 06:34 AM 5/7/2003 +0200, Sascha Schumann wrote:
> > What you are saying is that differentiating between a null value from > > next() and not having more elements is impossible without such a function? > > (I just want to be sure I understand what your motivation is). > > I don't really have a problem with array_has_more() the only problem is the > > name is not consistent with the other methods but on the other hand we > > don't allow new functions which don't follow the naming conventions. > > Catch-22? :) > > The key() can never become false which is why array_has_more > is not needed.
Never mind, I see what you're saying. Using === false would do the trick. Andi

(Marcus Börger)

23 years ago
Hello Andi, there was nothing in this thread that was against adding it besides Sascha suggestion that following code would do the trick: function array_has_more($ar) { if (!is_array($ar)) { trigger_error("Argument to array_has_more is no array", E_WARNING); return NULL; } next($ar); $more = key($ar) !== false; prev($ar); return $more; } Anyway you said that you don't like the name really. Do you have a better name? If not i'll go with 'array_has_more'. regards marcus Friday, July 4, 2003, 3:56:31 AM, you wrote: MB> Hello internals, MB> during my todays work i was again reminded that still one array function is MB> missing namely array_has_more. This function will return true if there are MB> more elements in an array and false if not. The real problem is that next() MB> doesn't work with false. MB> A diff against PHP 5 is attached. If noone objects i'll commit this later on MB> friday. MB> Examle: MB> <?php MB> $ar = array(2,1,false,0); MB> $i = 0; MB> function show() { MB> global $ar, $i; MB> echo "loop: ";var_dump($i++); MB> echo "curr: ";var_dump(current($ar)); MB> echo "more: ";var_dump(array_has_more($ar)); MB> echo "curr: ";var_dump(current($ar)); MB> echo "next: ";var_dump(next($ar)); MB> echo "\n"; MB> } MB> show(); MB> show(); MB> show(); MB> show(); MB> show(); MB> show(); ?>>
-- Best regards, Marcus mailto:helly@php.net

Sterling Hughes

23 years ago
Just a note that I'd prefer a different name as well. I really don't see the use for this function though. If you need to see the next element in the iteration use next(), and then rewind it with prev(). -Sterling On Sat, 2003-07-05 at 06:57, Marcus Börger wrote:
> Hello Andi, > > there was nothing in this thread that was against adding it besides Sascha > suggestion that following code would do the trick: > > function array_has_more($ar) { > if (!is_array($ar)) { > trigger_error("Argument to array_has_more is no array", E_WARNING); > return NULL; > } > next($ar); > $more = key($ar) !== false; > prev($ar); > return $more; > } > Anyway you said that you don't like the name really. Do you have a better > name? If not i'll go with 'array_has_more'. > > regards > marcus > > Friday, July 4, 2003, 3:56:31 AM, you wrote: > > MB> Hello internals, > > MB> during my todays work i was again reminded that still one array function is > MB> missing namely array_has_more. This function will return true if there are > MB> more elements in an array and false if not. The real problem is that next() > MB> doesn't work with false. > > MB> A diff against PHP 5 is attached. If noone objects i'll commit this later on > MB> friday. > > MB> Examle: > MB> <?php > > MB> $ar = array(2,1,false,0); > MB> $i = 0; > > MB> function show() { > MB> global $ar, $i; > > MB> echo "loop: ";var_dump($i++); > MB> echo "curr: ";var_dump(current($ar)); > MB> echo "more: ";var_dump(array_has_more($ar)); > MB> echo "curr: ";var_dump(current($ar)); > MB> echo "next: ";var_dump(next($ar)); > MB> echo "\n"; > MB> } > > MB> show(); > MB> show(); > MB> show(); > MB> show(); > MB> show(); > MB> show(); > > ?>> > > > > > -- > Best regards, > Marcus mailto:helly@php.net
-- "A business that makes nothing but money is a poor kind of business." - Henry Ford

Sascha Schumann

23 years ago
On Sat, 5 Jul 2003, Marcus [x-unknown] Börger wrote:
> Hello Andi, > > there was nothing in this thread that was against adding it besides Sascha > suggestion that following code would do the trick:
There was also nothing in favor of it. Beside the cludgy name, array loops can simply be written using either one of these notations: while (list(..) = each($foo)) while (key($foo) !== false) foreach ($foo as ..) I don't see the need to add another API to this list. - Sascha

Jason Greene

23 years ago
I do this exact thing all the time, and have never encountered any difficulties. -Jason On Sat, 2003-07-05 at 09:55, Sascha Schumann wrote:
> On Sat, 5 Jul 2003, Marcus [x-unknown] Börger wrote: > > > Hello Andi, > > > > there was nothing in this thread that was against adding it besides Sascha > > suggestion that following code would do the trick: > > There was also nothing in favor of it. Beside the cludgy > name, array loops can simply be written using either one of > these notations: > > while (list(..) = each($foo)) > while (key($foo) !== false) > foreach ($foo as ..) > > I don't see the need to add another API to this list. > > - Sascha
-- Jason Greene <jason@inetgurus.net> <jason@php.net>

Greg Beaver

23 years ago
Hi, How about array_has_next() to reference the php function? Greg Marcus BöRger wrote:

Andi Gutmans

23 years ago
The problem isn't the ending. The problem is that according to our naming conventions it should be array_foobar() whereas the other array functions were invented before our conventions and are short names such as next(). Now on one hand we wouldn't want to pollute the PHP function space with new non-conforming function names but on the other hand it is really ugly to have a new function which looks completely different from the original ones... Andi At 01:25 PM 5/7/2003 -0400, Greg Beaver wrote:

James Cox

23 years ago
> > The problem isn't the ending. The problem is that according > to our naming conventions it should be array_foobar() whereas the
other
> array functions were invented before our conventions and are short
names such
> as next(). Now on one hand we wouldn't want to pollute the PHP
function
> space with new non-conforming function names but on the other hand it
is
> really ugly to have a new function which looks completely different
from the
> original ones... >
You could turn around it's output and call it array_complete, so: if (array_complete($arr)) { return "no more elements"; } else { return "more elements available"; }