xiao Posted April 19, 2008 Share Posted April 19, 2008 I have 2 arrays: $_SESSION['geheugenBussen'] = array("DDR2-533","DDR2-667","DDR2-800","DDR2-900","DDR2-1000","DDR2-1066","DDR2-1100 en hoger","DDR2-1333"); $_SESSION['geheugenBussenCat'] = array("417","423","424","425","428","427","429","1277"); Each value in $_SESSION['geheugenBussen'] corresponds to 1 value in $_SESSION['geheugenBussenCat'] (so DDR2-667 = 423) I want to create a select list like this: <select...> <?php echo "<option value=\"" . $_SESSION['geheugenBussen'] . "\">" . $_SESSION['geheugenBussenCat'] . "</option"; ?> </select> But I need to output them all. How do I do this? Or is there a better way to make my arrays? Quote Link to comment https://forums.phpfreaks.com/topic/101837-solved-loop-through-2-arrays/ Share on other sites More sharing options...
chigley Posted April 19, 2008 Share Posted April 19, 2008 <?php $_SESSION['geheugenBussen'] = array("DDR2-533","DDR2-667","DDR2-800","DDR2-900","DDR2-1000","DDR2-1066","DDR2-1100 en hoger","DDR2-1333"); $_SESSION['geheugenBussenCat'] = array("417","423","424","425","428","427","429","1277"); echo "<select>\n"; foreach($_SESSION['geheugenBussen'] as $key => $value) { echo " <option value=\"{$value}\">{$_SESSION['geheugenBussenCat'][$key]}</option>\n"; } echo "</select>"; ?> ^ Try that. Quote Link to comment https://forums.phpfreaks.com/topic/101837-solved-loop-through-2-arrays/#findComment-521151 Share on other sites More sharing options...
wildteen88 Posted April 19, 2008 Share Posted April 19, 2008 You could use array_combine to combine the two arrays together. <?php $_SESSION['geheugenBussen'] = array("DDR2-533","DDR2-667","DDR2-800","DDR2-900","DDR2-1000","DDR2-1066","DDR2-1100 en hoger","DDR2-1333"); $_SESSION['geheugenBussenCat'] = array("417","423","424","425","428","427","429","1277"); $combined = array_combine($_SESSION['geheugenBussen'], $_SESSION['geheugenBussenCat']); echo "<select>\n"; foreach($combined as $key => $value) { echo " <option value=\"{$value}\">$key</option>\n"; } echo "</select>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/101837-solved-loop-through-2-arrays/#findComment-521155 Share on other sites More sharing options...
xiao Posted April 19, 2008 Author Share Posted April 19, 2008 thanks Quote Link to comment https://forums.phpfreaks.com/topic/101837-solved-loop-through-2-arrays/#findComment-521157 Share on other sites More sharing options...
chigley Posted April 19, 2008 Share Posted April 19, 2008 You could use array_combine to combine the two arrays together. <?php $_SESSION['geheugenBussen'] = array("DDR2-533","DDR2-667","DDR2-800","DDR2-900","DDR2-1000","DDR2-1066","DDR2-1100 en hoger","DDR2-1333"); $_SESSION['geheugenBussenCat'] = array("417","423","424","425","428","427","429","1277"); $combined = array_combine($_SESSION['geheugenBussen'], $_SESSION['geheugenBussenCat']); echo "<select>\n"; foreach($combined as $key => $value) { echo " <option value=\"{$value}\">$key</option>\n"; } echo "</select>"; ?> Except you'd need to swap the $key and $value around : <?php $_SESSION['geheugenBussen'] = array("DDR2-533","DDR2-667","DDR2-800","DDR2-900","DDR2-1000","DDR2-1066","DDR2-1100 en hoger","DDR2-1333"); $_SESSION['geheugenBussenCat'] = array("417","423","424","425","428","427","429","1277"); $combined = array_combine($_SESSION['geheugenBussen'], $_SESSION['geheugenBussenCat']); echo "<select>\n"; foreach($combined as $key => $value) { echo " <option value=\"{$key}\">{$value}</option>\n"; } echo "</select>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/101837-solved-loop-through-2-arrays/#findComment-521158 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.