Req #60684 [Com]: Add 'last' & 'first' blocks next to 'foreach' block
| From: | phristen at yahoo dot com | Date: | Sat, 05 May 2012 03:34:10 +0000 |
| Subject: | Req #60684 [Com]: Add 'last' & 'first' blocks next to 'foreach' block | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-170208@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=60684&edit=1
ID: 60684
Comment by: phristen at yahoo dot com
Reported by: aram_alipoor2010 at yahoo dot com
Summary: Add 'last' & 'first' blocks next to 'foreach'
block
Status: Open
Type: Feature/Change Request
Package: Arrays related
Operating System: All
PHP Version: 5.4.0RC5
Block user comment: N
Private report: N
New Comment:
I'm onboard with loop { } else { } construct, but this is an overkill.
I'd much rather have 2 additional array functions: is_array_last(array $array) and
is_array_first(array $array)... Of course, I don't insist on these function names, but they
should basically return true or false when array pointer is at the first position, or at the last
position.
Previous Comments:
------------------------------------------------------------------------
[2012-01-08 06:41:49] aram_alipoor2010 at yahoo dot com
Description:
------------
There are a lot of situations that we want our foreach loop act different for first and/or last
item. It will be so easy if php add these to concepts as blocks next to foreach. Compiler will run
'first' block for first item, and 'last' block for last item, and normal
'foreach' block for anything in between. The only special case is when array has only one
item, in this situation only 'first' block will act. See example at test script.
Test script:
---------------
foreach($array as $key => $value)
{
$str .= $key . ' = "'.$value.'",';
}
first
{
$str .= '(' . $key . ' = "'.$value.'",';
}
last
{
$str .= $key . ' = "'.$value.'")';
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=60684&edit=1