是否可以在没有支持数组的情况下清理关联数组的键和值?

时间:2015-09-01 12:49:00

标签: php arrays associative-array sanitization

当我有来自用户输入的数据(以关联数组('key'=>'value')的形式),我将不得不插入到数据库中时,我通常用来清理是:

<?php 
  foreach ($input as $key => &$value){ //Note the pass-by-reference
    $value = strip_tags($value);
    $value = htmlentities($value);
    $value = stripslashes($value);
    $value = mysqli_real_escape_string($link,$value);
  }
?>

但是,不允许通过引用传递,因此,如果我必须清除键值,我所做的是:

<?php
  $sanitizedInput = array(); 
  foreach ($input as $key => $value){
    $key = strip_tags($key); 
    $key = htmlentities($key);
    $key = stripslashes($key);
    $key = mysqli_real_escape_string($link,$key);
    $value = strip_tags($value);
    $value = htmlentities($value);
    $value = stripslashes($value);
    $value = mysqli_real_escape_string($link,$value);
    $sanitizedInput[$key] = $value;
  }
?>

当然这有效,但第一种方式的优点是我不需要额外的数组作为支持变量。 是否有可能为密钥清理实现相同的简单性但不使用支持变量?

0 个答案:

没有答案