RE: [PHP] Confusing array question ...
Try adding curly braces around the var name, like this: for($i=0; $icount(${beginning . $value . end}); $i++) { also, a look at the variable variables manual page will help further HTH Martin -Original Message- From: John Kelly [mailto:[EMAIL PROTECTED]] Sent: Wednesday, September 11, 2002 9:27 AM To: [EMAIL PROTECTED] Subject: [PHP] Confusing array question ... Hi, I have 1 primary array called $item containing 20 values each of whose value is part of the name of 20 other arrays containing various number of values. How can I cycle through the primary $item array using a foreach and within that do a for on each of the 20 secondary arrays dynamically inserting each of the 20 secondary array names based on the value of the current primary $item array. Something like the following which obviously does not work. ?php foreach($item as $value) { // do some stuff for($i=0; $icount($beginning_of_array_name_always_the_same . $value . end_of_array_name_always_the_same); $i++) { // do some stuff } } ? I am trying to avoid doing something like the following ... ?php foreach($item as $value) { // do some stuff if($value == 'red'){ $array = $temp_red_file; } elseif ($value == 'green'){ $array = $temp_green_file; // and so on for all 20 possibilities } for($i=0; $icount($array); $i++) { // do some stuff } } ? Thanks! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Confusing array question ...
Thanks that did it! Martin Towell [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Try adding curly braces around the var name, like this: for($i=0; $icount(${beginning . $value . end}); $i++) { -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Confusing array question ...
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Hey John, Actually, you don't seem to far off. You can use a string as a variable name by enclosing the string in ${ }. i.e: ${foo} You should be able to write your loop as something like: foreach($item as $value) { // do some stuff foreach(${prefix${value}suffix} as $v) { // do some stuff } } All of the normal string and variable rules apply, so you can do things like: echo ${ some . $bar .text}; ${prefix${baz}suffix} = 10; ${md5($quux)) = $quux; There is also the added behavior (benefit?) of being able to use odd values for variable names: echo ${ \n\t} = Holy Whitespace, Batman!; HTH! - --zak On September 10, 2002 17:26, John Kelly wrote: Hi, I have 1 primary array called $item containing 20 values each of whose value is part of the name of 20 other arrays containing various number of values. How can I cycle through the primary $item array using a foreach and within that do a for on each of the 20 secondary arrays dynamically inserting each of the 20 secondary array names based on the value of the current primary $item array. Something like the following which obviously does not work. ?php foreach($item as $value) { // do some stuff for($i=0; $icount($beginning_of_array_name_always_the_same . $value . end_of_array_name_always_the_same); $i++) { // do some stuff [chop] -BEGIN PGP SIGNATURE- Version: GnuPG v1.0.7 (GNU/Linux) iD8DBQE9frG7b6QONwsK8bIRAlehAJ9ivM6lWm3mu68Gy43vKKPvmZo+CgCfdBdf 5IikMTwLaBwKPvoFDz4CfD0= =CjQD -END PGP SIGNATURE- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Confusing array question ...
Thanks for the examples Zak! I got it working. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php