Hi,
trying out the following source I was astonished to see that using
list() does not work directly in a foreach() but works fine if used
through a temporary variable.
Would allowing using list() inside the foreach()-statement be desired
behaviour? Or is it just me? :-)
$a = array(
array(5, 'A'),
array(3, 'B'),
);
foreach($a as list($k, $v)) {
echo $k;
}
Expected output: 53
Result: syntax error, unexpected 'list' (T_LIST) in ...
Works:
foreach($a as $b) {
list($k, $v) = $b;
echo $k;
}
Kind regards,
Stefan
Hi,
trying out the following source I was astonished to see that using
list() does not work directly in a foreach() but works fine if used
through a temporary variable.
Hi,
This feature was already voted in and accepted.
https://wiki.php.net/rfc/foreachlist#vote
It has been implemented in PHP 5.5
Would allowing using list() inside the foreach()-statement be desired
behaviour? Or is it just me? :-)$a = array(
array(5, 'A'),
array(3, 'B'),
);
foreach($a as list($k, $v)) {
echo $k;
}Expected output: 53
Result: syntax error, unexpected 'list' (T_LIST) in ...
Works:
foreach($a as $b) {
list($k, $v) = $b;
echo $k;
}Kind regards,
Stefan
Hi,
trying out the following source I was astonished to see that using
list() does not work directly in a foreach() but works fine if used
through a temporary variable.Hi,
This feature was already voted in and accepted.
https://wiki.php.net/rfc/foreachlist#voteIt has been implemented in PHP 5.5
Thanks for the quick reply and sorry for the noise.
I didn't follow that closely.
Nice to see my thoughts were correct this would be logical :-)
Regards,
Stefan
Hi,
trying out the following source I was astonished to see that using
list() does not work directly in a foreach() but works fine if used
through a temporary variable.Would allowing using list() inside the foreach()-statement be desired
behaviour? Or is it just me? :-)
Support for this has been added in PHP 5.5. RFC:
https://wiki.php.net/rfc/foreachlist
Nikita