ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> php-dev
php-dev
#44953 [Opn->Bgs]: Variable variables dont work in foreach
by colder other posts by this author
May 9 2008 3:28AM messages near this date
#44953 [NEW]: Variable variables dont work in foreach | #44953 [Opn]: Variable variables dont work in foreach
ID:               44953
 Updated by:       colder@[...].net
 Reported By:      ies_clan at hotmail dot com
-Status:           Open
+Status:           Bogus
 Bug Type:         Variables related
 Operating System: Linux
 PHP Version:      5.2.6
 New Comment:

Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

This is expected:
The evaluation of ${$key} in your foreach is done at each loop.
So you're overwriting $key with $$key as $key is initially "key". 

$key 	= 'otherkey';
$value 	= 'othervalue';
foreach($array AS ${$key} =>  ${$value}){
	echo ${$key}." -\n"; // echo $otherkey." -\n"
}

will work just fine.


Previous Comments:
------------------------------------------------------------------------

[2008-05-09 10:27:37] ies_clan at hotmail dot com

oh sry i interchanges the field...

Expected result:
----------------
0
1
2

Actual result:
--------------
1
2

------------------------------------------------------------------------

[2008-05-09 09:58:03] ies_clan at hotmail dot com

Description:
------------
If you try to use Variable variables in a foreach, the output of the
array is wrong

Reproduce code:
---------------
$array = Array(
	0 =>  Array('11', '12', '13'), 
	1 =>  Array('21', '22', '23'), 
	2 =>  Array('31', '32', '33')
);
$key 	= 'key';
$value 	= 'value';
foreach($array AS ${$key} =>  ${$value}){
	echo ${$key}." -\n";
}

Expected result:
----------------
1
2

Actual result:
--------------
0
1
2


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=44953&edit=1
Thread:
Ies_clan At Hotmail Dot Com
colder
Ies_clan At Hotmail Dot Com

Privacy Policy | Email Opt-out | Feedback | Syndication
© 2004 ActiveState, a division of Sophos All rights reserved