简体   繁体   English

foreach和数组映射错误

[英]foreach and array maps error

I'm getting: 3 warnings (Illegal offset type in isset or empty in...) 3 notice (Undefined index: ...) 我收到:3警告(isset中的偏移量类型非法或in ...中的空偏移量)3通知(未定义索引:...)

The 3 warnings are realated with the first foreach. 这三个警告是通过第一个foreach实现的。 And the 3 notices are related with the second foreach loop. 这3个通知与第二个foreach循环有关。

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. 谢谢你们。

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

Instead use foreach ($expected as $key => $value) { and foreach($result as $key=>$value) { if (!$value) die(false); } 而是使用foreach ($expected as $key => $value) {foreach($result as $key=>$value) { if (!$value) die(false); } 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. 如果您的目标是验证输入,那么我建议您不要大量使用PHP清理机制。 Like all PHP, it is founded on the fundamentally broken philosophy of sanitizing input rather than validating it. 像所有的PHP,它是建立在消毒输入,而不是验证它的根本破理念。 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. 尊重/验证看起来很有希望,尽管我认为如果可以的话,最好离开PHP。

Making do with what we have, use the following function with only FILTER_VALIDATE_* filters. 根据现有情况,仅将以下函数与FILTER_VALIDATE_*过滤器一起使用。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM