dongyu4863 2015-12-21 22:42
浏览 39
已采纳

为每个嵌套多个输出

I'm having a hard time filtering out the excess "option" outputs here. I'm thinking I'm confusing myself.

foreach ($bv_wg_lf_rf_array_base as $bv_wg_lf_rf_arrayaaa) {
    foreach ($bv_wg_lf_rf_array as $base) {
        if ($bv_wg_lf_rf_arrayaaa == $base){
            $bv_wg_lf_rf_arrayaaa = strtoupper($bv_wg_lf_rf_arrayaaa);
            $me .= '<option value="'.$bv_wg_lf_rf_arrayaaa.'" selected>'
                   .$bv_wg_lf_rf_arrayaaa.'</option>';
        }   
        else {
            $base = strtoupper($bv_wg_lf_rf_arrayaaa);
            $me .= '<option value="'.$bv_wg_lf_rf_arrayaaa.'">'
                   .$bv_wg_lf_rf_arrayaaa.'</option>';
        }

    }
}
echo $me;

This on dump returns (without excess)

WG
WG
WG
LF
LF
LF
RF
RF
RF

bv_wg_lf_rf_array_base =

array (size=3)
  0 => string 'WG' (length=2)
  1 => string 'LF' (length=2)
  2 => string 'RF' (length=2)

bv_wg_lf_rf_array

array (size=3)
  0 => string 'RF' (length=2)
  1 => string '' (length=0)
  2 => string '' (length=0)

The first array is a manually created array to determine the actual inputs while the second array is from the database. Three different columns for WG LF and RF is present(else null in db).

So basically it's spitting it out all three times rather than creating the selected option then erasing skipping and moving to the others that should be without selected.

  • 写回答

1条回答 默认 最新

  • doujiang1001 2015-12-21 23:01
    关注

    First, you can reduce the array that comes from the database to a single value like this:

    $selected = array_filter($bv_wg_lf_rf_array)[0];
    

    Using array_filter without a callback will remove all of the empty string values from your array.

    If you have an older PHP version, it may need to be done in two statements:

    $selected_array = array_filter($bv_wg_lf_rf_array);
    $selected = $selected_array[0];
    

    It looks like this could be avoided if you changed the query that produces this array to only get the selected value from the database, but I am just working with what you have here.

    This will significantly simplify building your option string.

    foreach ($bv_wg_lf_rf_array_base as $value) {
        // see if the value matches the selection
        $selected = ($value == $selected) ? 'selected' : '';
        // append the option with the appropriate 'selected' setting
        $me .= "<option value=\"$value\" $selected>$value</option>";
    }
    

    If you need to handle multiple selections, you can just skip the array_filter and use in_array to check your selections like this:

    foreach ($bv_wg_lf_rf_array_base as $value) {
        $selected = (in_array($value, $bv_wg_lf_rf_array)) ? 'selected' : '';
        $me .= "<option value=\"$value\" $selected>$value</option>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。