fj1200 Posted July 8, 2009 Share Posted July 8, 2009 Can anyone assist with this? I need the code to select between 2 arrays and then display the results accordingly but it's not working... $arr_tower = ""; if ($FOLDER == "101") $arr_tower = ('$kba'); if ($FOLDER == "201") $arr_tower = ('$kba'); if ($FOLDER == "301") $arr_tower = ('$uni'); if ($FOLDER == "401") $arr_tower = ('$uni'); echo 'FOLDER = '. $FOLDER . ' so Tower = '. $arr_tower; echo ' --- foreach ('.$arr_tower.' as $key=>$tower)'; $kba = array(0=>"U1/2", 2=>"T2A", 3=>"U3", 4=>"U5/6", 5=>"T7"); $uni = array(0=>"T1", 1=>"T2", 2=>"T3", 3=>"T4", 4=>"T5", 5=>"T6", 6=>"T7", 7=>"T8", 8=>"T9", 9=>"T10"); echo "<table>"; foreach ($arr_tower as $key=>$tower) { $k = key($arr_tower); $val = $key + '15'; echo '<tr><td>'. $key .' => '. $tower. ' = '. $val .' --> '. $pbupdate[$val].'</td></tr>'; }; echo "</table>"; Got to be something simple. If I just use one array and the array name rather than $arr_tower it works fine. (btw - I'm adding 15 to $key because the db query result for this section starts at $pbupdate[15] and it works.) This bit I added in as a test to see what exactly was being produced- echo 'FOLDER = '. $FOLDER . ' so Tower = '. $arr_tower; echo ' --- foreach ('.$arr_tower.' as $key=>$tower)'; - produces the correct string... FOLDER = 401 so Tower = $uni --- foreach ($uni as $key=>$tower) ...however the web page shows this error: "Warning: Invalid argument supplied for foreach() in C:\wamp\www\ProdBoard\pb_job_update.php on line 245" ...which is the actual foreach() line. I've tried moving the arrays to the top of the section; to where they are now; all oevr the place... is there something I'm missing here? Link to comment https://forums.phpfreaks.com/topic/165162-confused-with-choice-of-2-arrays-not-working/ Share on other sites More sharing options...
ignace Posted July 8, 2009 Share Posted July 8, 2009 foreach ($arr_tower as $key=>$tower) gives: foreach ('$uni' as $key => $tower) which gives you: Warning: Invalid argument supplied for foreach() in C:\wamp\www\ProdBoard\pb_job_update.php on line 245 use the next technique instead (http://us3.php.net/manual/en/language.variables.variable.php): if ($FOLDER == "101") $arr_name = 'kba'; if ($FOLDER == "201") $arr_name = 'kba'; if ($FOLDER == "301") $arr_name = 'uni'; if ($FOLDER == "401") $arr_name = 'uni'; $kba = array(0=>"U1/2", 2=>"T2A", 3=>"U3", 4=>"U5/6", 5=>"T7"); $uni = array(0=>"T1", 1=>"T2", 2=>"T3", 3=>"T4", 4=>"T5", 5=>"T6", 6=>"T7", 7=>"T8", 8=>"T9", 9=>"T10"); $arr_tower = ${$arr_name}; foreach ($arr_tower as $key => $tower) { .. Link to comment https://forums.phpfreaks.com/topic/165162-confused-with-choice-of-2-arrays-not-working/#findComment-870868 Share on other sites More sharing options...
sasa Posted July 8, 2009 Share Posted July 8, 2009 try <?php $arr_tower = ""; //$FOLDER=401; if ($FOLDER == "101") $arr_tower = ('kba'); if ($FOLDER == "201") $arr_tower = ('kba'); if ($FOLDER == "301") $arr_tower = ('uni'); if ($FOLDER == "401") $arr_tower = ('uni'); echo 'FOLDER = '. $FOLDER . ' so Tower = '. $arr_tower; echo ' --- foreach ('.$arr_tower.' as $key=>$tower)'; $kba = array(0=>"U1/2", 2=>"T2A", 3=>"U3", 4=>"U5/6", 5=>"T7"); $uni = array(0=>"T1", 1=>"T2", 2=>"T3", 3=>"T4", 4=>"T5", 5=>"T6", 6=>"T7", 7=>"T8", 8=>"T9", 9=>"T10"); echo "<table>"; foreach ($$arr_tower as $key=>$tower) { $k = key($$arr_tower); $val = $key + '15'; echo '<tr><td>'. $key .' => '. $tower. ' = '. $val .' --> '. $pbupdate[$val].'</td></tr>'; }; echo "</table>"; ?> Link to comment https://forums.phpfreaks.com/topic/165162-confused-with-choice-of-2-arrays-not-working/#findComment-870869 Share on other sites More sharing options...
fj1200 Posted July 8, 2009 Author Share Posted July 8, 2009 Sasa's one worked - in a test output I got FOLDER = 401 so Tower = uni --- foreach (uni as $key=>$tower)0 => T1 = 15 --> 0 1 => T2 = 16 --> 0 2 => T3 = 17 --> 0 3 => T4 = 18 --> 0 4 => T5 = 19 --> 0 5 => T6 = 20 --> 0 6 => T7 = 21 --> 0 7 => T8 = 22 --> 0 8 => T9 = 23 --> 0 9 => T10 = 24 --> 0 ..and so I can do stuff with that. Ignace - sorry - your one produced the same error, but they've both helped me a lot. Never come across variable variables before - but then I'm no web programmer, just a sysadmin, but learning and using php to write tools for the job - and enjoying it (mostly). Link to comment https://forums.phpfreaks.com/topic/165162-confused-with-choice-of-2-arrays-not-working/#findComment-870906 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.