php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #10203 allow foreach($array as list($a,$b)
Submitted: 2001-04-06 06:53 UTC Modified: 2012-09-21 12:40 UTC
Votes:21
Avg. Score:3.7 ± 1.0
Reproduced:16 of 18 (88.9%)
Same Version:9 (56.2%)
Same OS:9 (56.2%)
From: jack at mobil dot cz Assigned: laruence (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: * OS: *
Private report: No CVE-ID: None
 [2001-04-06 06:53 UTC] jack at mobil dot cz
hello

foreach ($array as list($a, $b))
	;

won't work. is there any hope this will change in future?

Patches

php-5.4.0svn-add-foreach-list-support.patch (last revision 2011-07-06 12:15 UTC by laruence@php.net)
php-5.4.0-alpha1-foreach-list-diff.patch (last revision 2011-07-06 05:34 UTC by laruence@php.net)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-04-06 07:19 UTC] hholzgra@php.net
and what should it do?

i guess you want to have key and value in one step?

this is done by the foreach($array as $key => $value)
construct, does that satisfy your needs?
 [2001-04-06 07:54 UTC] jack at mobil dot cz
no, 'list' should assign an array to a list of variables.

$arr = array(array(1, 2), array(3, 4));

foreach ($arr as list($a, $b))
	echo "$a $b\n";

 [2001-04-06 14:42 UTC] torben@php.net
It's for someone else to decide whether to add this to 
the language, but until they do you can achieve the same
effect with:

$arr = array(array(1, 2), array(3, 4)); 
foreach ($arr as $narr) {
    list($a, $b) = array_values($narr);
    echo "$a, $b\n";
}

BTW--it would have helped if you had explained what 
results you were after and not just the syntax you wanted. ;)
 [2002-01-28 20:45 UTC] yohgaki@php.net
foreach ($arr as $key => $val)

 [2002-01-29 03:34 UTC] jack at mobil dot cz
you haven't read the previous posts, have you?

I think it's a general problem with list() construct, that you cannot use it everywhere you can use an L-value (or however you guys call it in PHP)
 [2002-01-29 04:19 UTC] yohgaki@php.net
It's in TODO already :)
Someone might implement it, but I guess it's not anytime soon.
 [2010-12-01 16:08 UTC] jani@php.net
-Package: Feature/Change Request +Package: Scripting Engine problem -PHP Version: 4CVS, 5CVS (2004-04-13) +PHP Version: *
 [2011-07-06 01:34 UTC] laruence@php.net
The following patch has been added/updated:

Patch Name: php-5.4.0-alpha1-foreach-list-diff.patch
Revision:   1309930450
URL:        https://bugs.php.net/patch-display.php?bug=10203&patch=php-5.4.0-alpha1-foreach-list-diff.patch&revision=1309930450
 [2011-07-06 01:35 UTC] laruence@php.net
I am working on this now, add firsh patch is submitted.
 [2011-07-06 08:15 UTC] laruence@php.net
The following patch has been added/updated:

Patch Name: php-5.4.0svn-add-foreach-list-support.patch
Revision:   1309954555
URL:        https://bugs.php.net/patch-display.php?bug=10203&patch=php-5.4.0svn-add-foreach-list-support.patch&revision=1309954555
 [2011-07-06 11:20 UTC] laruence@php.net
-Status: Analyzed +Status: Assigned -Assigned To: +Assigned To: laruence
 [2011-08-19 11:57 UTC] laruence@php.net
-Status: Assigned +Status: Suspended
 [2011-08-19 11:57 UTC] laruence@php.net
since the RFC didn't win the vote, change the status to suspended.
 [2011-08-19 12:08 UTC] laruence@php.net
-Status: Suspended +Status: Wont fix
 [2012-07-19 07:17 UTC] stas@php.net
-Status: Wont fix +Status: Open
 [2012-09-21 12:40 UTC] laruence@php.net
The fix for this bug has been committed.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.

Implemented in php-trunk. https://wiki.php.net/rfc/foreachlist
 [2012-09-21 12:40 UTC] laruence@php.net
-Status: Assigned +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 13:01:29 2024 UTC