gosulove gosulove - 1 year ago 87
PHP Question

Input <select> selected is not working correctly in PHP by comparing 2 arrays

I have

3 <select></select>

with 8 options inside (Array_1 with 8 position). All options value are the same.

What i am trying to do is that I want to make the Value in Array_2 to be the Selected value.

However, looks like my loop is not working correctly.




echo '<select name="product_header_image[]">';

echo '<option selected VALUE="'.$array_1[$b].'" >'.$array_1[$b].'</option>';
echo '<option VALUE="'.$array_1[$b].'" >'.$array_1[$b].'</option>';

echo '</select>';
echo '<br>';


enter image description here

Anyone know whats wrong with my coding? what I want to achieve is the right hand side of the screenshot.

Answer Source

You need to replace this check:

if(in_array($array_2[$i], $array_1)) {

on this:

if($array_2[$i] == $array_1[$b]) {

Demo here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download