doujindou4356 2012-12-26 19:23
浏览 34
已采纳

foreach和数组映射错误

I'm getting: 3 warnings (Illegal offset type in isset or empty in...) 3 notice (Undefined index: ...)

The 3 warnings are realated with the first foreach. And the 3 notices are related with the second foreach loop.

I don't really get what I'm doing wrong... Need some help out here.

<form name="form" action="index.php" method="POST">
    <input type="text" name="name" value="<?php if (isset($_POST['name'])) { echo $_POST['name']; } ?>" />
    <input type="text" name="age" value="<?php if (isset($_POST['age'])) { echo $_POST['age']; } ?>" />
    <input type="text" name="email" value="<?php if (isset($_POST['email'])) { echo $_POST['email']; } ?>" />
    <input type="submit" />
</form>

<?php
$expected = array(
    'name' => array("filter" => FILTER_SANITIZE_STRING),
    'age' => array("filter" => FILTER_SANITIZE_NUMBER_INT),
    'email' => array("filter" => FILTER_SANITIZE_EMAIL)
);

foreach ($expected AS $key => $value) {
    if (!isset($_POST[$value])) {
        echo "not set";
    } elseif (empty($_POST[$value])) {
        echo "empty";
    }
}

$result = filter_input_array(INPUT_POST, $expected);

foreach ($result AS $key => $value) {
    if (!$result[$value]) {
        echo "not valid value";
    }
}
?>

Thank you all.

  • 写回答

1条回答 默认 最新

  • dongtuo7364 2012-12-26 19:33
    关注

    The forms of foreach are foreach($iterable as $value) and foreach($iterable as $key => $value). There is no foreach ($iterable as $key) form as with javascript's for (key in obj).

    Instead use foreach ($expected as $key => $value) { and foreach($result as $key=>$value) { if (!$value) die(false); }

    Your complete code should be something like this:

    $expected = array(
        'name' => array("filter" => FILTER_SANITIZE_STRING),
        'age' => array("filter" => FILTER_SANITIZE_NUMBER_INT),
        'email' => array("filter" => FILTER_SANITIZE_EMAIL)
    );
    
    foreach ($expected AS $key => $value) {
        if (!isset($_POST[$key])) {  // NOT $value!!!!
            echo "not set";
        } elseif (empty($_POST[$key])) {
            echo "empty";
        }
    }
    
    $result = filter_input_array(INPUT_POST, $expected);
    
    foreach ($result AS $key => $value) {
        if (!$result[$key]) {  // NOT $value!!!
            echo "not valid value";
        }
    }
    

    If your goal is to validate input, then I advise you not make heavy use of the PHP sanitize mechanism. Like all PHP, it is founded on the fundamentally broken philosophy of sanitizing input rather than validating it. I'm not sure what else to advise you to use, however. Respect/Validation looks promising, although I think you would be better off leaving PHP if you can.

    Making do with what we have, use the following function with only FILTER_VALIDATE_* filters. You will still need to do pre- and post-processing, and you have to emulate "chained" filters with multiple calls.

    function filter_array($data, $filter) {
        $missing = array_diff_key($filter, $data);
        $filtered = filter_var_array($data, $filter);
        $invalid = array_filter($filtered, function($v){return $v===FALSE;});
        $filtered = array_diff_key($filtered, $missing, $invalid);
        return array($filtered, array_keys($invalid), array_keys($missing));
    }
    

    Example in use:

    $_POST = array('extra'=>'extra', 'age'=>array('30a'), 'name'=>'the name');
    
    $expected = array(
        'name'   => array("filter" => FILTER_UNSAFE_RAW, // using this as a "passthrough" filter
                          "flags"  => FILTER_REQUIRE_SCALAR, // just to set this flag
        ),
        'age'   => array("filter"  => FILTER_VALIDATE_INT,
                         "flags"   => FILTER_REQUIRE_SCALAR,
                         "options" => array('min_range'=>0, 'max_range'=>120)
        ),
        'email' => array("filter"  => FILTER_VALIDATE_EMAIL,
                         "flags"   => FILTER_REQUIRE_SCALAR,
        ),
    );
    
    list($valid, $invalid, $missing) = filter_array($_POST, $expected);
    
    var_export($valid); var_export($invalid); var_export($missing);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)