Req #60684 [Opn->Sus]: Add 'last' & 'first' blocks next to 'foreach' block
Edit report at https://bugs.php.net/bug.php?id=60684&edit=1
ID: 60684
Updated by: cmb@php.net
Reported by: aram_alipoor2010 at yahoo dot com
Summary: Add 'last' & 'first' blocks next to 'foreach'
block
-Status: Open
+Status: Suspended
Type: Feature/Change Request
-Package: Arrays related
+Package: Scripting Engine problem
Operating System: All
PHP Version: 5.4.0RC5
Block user comment: N
Private report: N
New Comment:
Anyhow, this change would require the RFC process[1], so I'm suspending this feature request
for the time being. Note that there had been a similar RFC[2], which had been declined.
[1] <https://wiki.php.net/rfc/howto>
[2] <https://wiki.php.net/rfc/loop_or>
Previous Comments:
------------------------------------------------------------------------
[2016-06-14 17:53:18] cmb@php.net
In my opinion, the given use case is doubtful. I might write it as:
<?php
$lines = array();
foreach ($array as $key => $value) {
$lines[] = $key . ' = "' . $value . '"';
}
$str = '(' . explode(',', $lines) . ')';
?>
------------------------------------------------------------------------
[2012-05-05 03:34:10] phristen at yahoo dot com
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.
------------------------------------------------------------------------
[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
Thread (4 messages)