andyd34 Posted February 2, 2010 Share Posted February 2, 2010 I have a fuction that uses an array to populate and select list $fruit = array( '1' => 'apple', '2' => 'pear', '3' => 'orange', '4' => 'banana', '5' => 'clementine', '6' => 'kiwi', ); I then have the following for($i=1 ; $ <= 6 ; $i++) { $value = "<option value='$fruit[$i]>$fruit[$i]</option>"; } What I want though is the value of the option to be the index of the array, does anyone have any ideas Link to comment https://forums.phpfreaks.com/topic/190663-array-is-function/ Share on other sites More sharing options...
gwolgamott Posted February 2, 2010 Share Posted February 2, 2010 for($i=1 ; $ <= 6 ; $i++) { $value = "<option value='$fruit[$i]>$fruit[$i]</option>"; } What I want though is the value of the option to be the index of the array, does anyone have any ideas So you want the value to be the increment number? as in array[1] = "apple" you want "1" to be the value? if so... for($i=1 ; $ <= 6 ; $i++) { $value = "<option value='".$i."'>".$fruit[$i]."</option>"; } Link to comment https://forums.phpfreaks.com/topic/190663-array-is-function/#findComment-1005504 Share on other sites More sharing options...
jl5501 Posted February 2, 2010 Share Posted February 2, 2010 or foreach ($fruit as $k => $v) { $value = "<option value='".$k."'>".$v."</option>"; } Link to comment https://forums.phpfreaks.com/topic/190663-array-is-function/#findComment-1005509 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.