dss89001 2014-10-06 11:44
浏览 36
已采纳

在同一个数组上使用filter_input_array和array_filter

I have a variable set of identical input fields in my form where some can be empty.

<input type="text" class="form-control" name="option[]" placeholder="fill in" />
<input type="text" class="form-control" name="option[]" placeholder="optional" />
<input type="text" class="form-control" name="option[]" placeholder="optional" />

the values of these input fields come into the array $_POST["option"]

I want to filter $_POST["option"] and for that I am using the following code:

$filter = array("option" => array("filter"=>FILTER_CALLBACK,"flags"=>FILTER_FORCE_ARRAY,"options"=>"ucwords"));
$optionfin = filter_input_array(INPUT_POST, $filter);

But now I want to also use the array_filter function to remove the empty fields so I can INPUT only the filled in fields into my database but this doesn't seem to work.

$option = array_filter($optionfin);

when I do the following:

$optionfin = array_filter($_POST["option"]);

this is working fine, but i know you better not access the $_POST this way because it isn't that safe...

Can anyone help me to point out where i am going wrong?

Thanks

  • 写回答

1条回答 默认 最新

  • drci47425 2014-10-06 13:23
    关注

    Because you are using filter_input_array, you will actually end up with an associative array that corresponds to $_POST. So to filter out empty fields as you are trying to do, you will need to specify the option key:

    $option = array_filter($optionfin['option']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c