繁体   English   中英

如何在PHP中将2个参数传递给array_map函数?

[英]How do I pass 2 arguments to an array_map function in php?

$sql="select * from question_test where test_id='".$test_id."'   and  difficulty_level   BETWEEN  ".$_SESSION['difficulty_start']." and  ".$_SESSION['difficulty_end']." and question_id NOT IN ('".implode("', '", array_map('mysqli_real_escape_string', $_SESSION['question_attempt']))."')  order by rand()*favourability_level  desc";

在运行上面的代码时,我得到一个错误:

警告:mysqli_real_escape_string()恰好需要2个参数,第359行的C:\\ xampp \\ htdocs \\ exam.php中给出了1个参数

改用回调函数

function array_map_callback($a)
{
  global $con;
  return mysqli_real_escape_string($con, $a);
}

array_map('array_map_callback', $_SESSION['question_attempt']);

其中$con是连接变量。

因此,您的$sql变量为:

$sql="select * from question_test where test_id='".$test_id."' and  difficulty_level BETWEEN  ".$_SESSION['difficulty_start']." and  ".$_SESSION['difficulty_end']." and question_id NOT IN ('".implode("', '", array_map('array_map_callback', $_SESSION['question_attempt']))."')  order by rand()*favourability_level  desc";

或者你可以选择array_walk

array_walk($_SESSION['question_attempt'], function(&$string) use ($con) { 
  $string = mysqli_real_escape_string($con, $string);
});

暂无
暂无

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

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