如何动态设置在PHP中选择的选项

I have the following code.. I loop through an array. Now I want to check each value against a post (in my special case via $_REQUEST) variable.. If they are equal the option field should be marked as selected..

$optionArr = array (opt_side_a => 10, opt_side_b => 20);
// Outter LOOP - $cside = iterator //
// INNER LOOP // 
foreach($optionArr as $key => $value){
    if($_REQUEST['opt_side_'.$cside] == $value){
        $isSelected = "selected";
    }
    echo '<option value="'.$value.'"'.$isSelected.'>'.$key.'</option>';
}

My problem is that every option value is marked as selected.

Try this

foreach($optionArr as $key => $value){
   $isSelected =""; //added this line
   if($_REQUEST['opt_side_'.$cside] == $value){
     $isSelected = "selected";
   }
   echo '<option value="'.$value.'"'.$isSelected.'>'.$key.'</option>';
}

You should do this in the next way:

$optionArr = array (opt_side_a => 10, opt_side_b => 20);
// Outter LOOP - $cside = iterator //
// INNER LOOP // 
foreach($optionArr as $key => $value){
    if($_REQUEST['opt_side_'.$cside] == $value){
        echo '<option value="'.$value.'" "selected">'.$key.'</option>';
    }

}

You set $isSelected only once but it will display in all options after it was initialized for the first time. Also if you need this variable you shoud clean it up after setting option selected once like this:

$optionArr = array (opt_side_a => 10, opt_side_b => 20);
// Outter LOOP - $cside = iterator //
// INNER LOOP // 
foreach($optionArr as $key => $value){
    $isSelected = "";    
    if($_REQUEST['opt_side_'.$cside] == $value){
        $isSelected = "selected";
    }
    echo '<option value="'.$value.'"'.$isSelected.'>'.$key.'</option>';
}