doumu9019 2012-11-10 16:04
浏览 131
已采纳

如何从PHP foreach循环中排除隐藏字段

I have a simple form that inserts data into a database using foreach($_POST as $key=>$value) I have a hidden field on the form

<input name="isset" type="hidden" value="true" />

And i use if(isset($_POST['isset'])) {

I'm trying to work out how to exclude the hidden field from the loop ...?

I've looked at this post but don't understand where i would use if (strpos($key, 'hdn_') == false) // proceed

How to exclude <input type="hidden"> from a for each loop in PHP

any guidance would be appreciated....

  • 写回答

2条回答 默认 最新

  • dongxie3701 2012-11-10 16:12
    关注

    If you know the exact names of keys you want to exclude, array_diff_key is a convenient option:

    $keysToRemove = array('isset'); // you can add as many as you want
    $values = array_diff_key($_POST, array_flip($keysToRemove));
    
    foreach ($values as $k => $v) { ... }
    

    However, since $values is intended to go into the database you should use a whitelist of allowed keys instead of a blacklist. You can do that with array_intersect_key:

    $keysToKeep = array('field1', 'field2', 'field3'); // as many as you want
    $values = array_intersect_key($_POST, array_flip($keysToKeep));
    
    foreach ($values as $k => $v) { ... }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路