Re: looping inside of a loop

From: Date: Wed, 11 Oct 2000 00:25:44 +0000
Subject: Re: looping inside of a loop
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-19449@lists.php.net to get a copy of this message
Rodney Davis writes: > I am trying to dynamically generate an option list with the > following script. easy enough but now I need the option > value and the displayed value to be different. The option > value must be in english and the displayed value must > be in Japanese. How do I get $values[0] below into > a loop inside of the original foreach loop so that > "something" and "somethinginjapanese" match? > > function writePrefsOptions () { > $prefs = array("something", "somethingelse"); > $values = array("somethinginjapanese", "somethingelseinjapanese"); > foreach ($prefs as $value) { > print "<OPTION VALUE=\"".($value). "\""; > print (($prefs == ($value))?"selected":""); > print ">$values[0]</option>\n"; > } > } > > i tried another foreach inside of the foreach loop and it didn't work > as anticipated. What else could i do? > > Thanks for your help in advance, > > rodney If feasible, you could have the English words as the keys of the array, and the Japanese as the values. Something like this (not tested but should be close): $prefs = array( 'something' => 'something_in_japanese', 'something_else' => 'something_else_in_japanese' ); while (list($option, $value) = each($prefs)) { $selected = $option == $selected ? 'selected' : ''; echo "<option value=\"$option\"$selected>$value</option>\n"; } That method automatically ensures that the option->value pairs are always synced up, so long as that one array doesn't get hosed. Maintaining two arrays means having to keep them in sync manually--depending on the application, this may or may not be an issue. If you can't do that and the arrays are always guaranteed to be in sync, you could just loop through one, keeping an integer counter as you go, and use that integer counter to index into the corresponding element of the other array. -- +----------------------------------------------------------------+ |Torben Wilson <torben@php.net> Netmill iTech| |http://www.coastnet.com/~torben http://www.netmill.fi| |Ph: 1 250 383-9735 torben@netmill.fi| +----------------------------------------------------------------+

« previous php.general (#19449) next »