![](/img/trans.png)
[英]array_map and pass 2 arguments to the mapped function - array_map(): Argument #3 should be an array
[英]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.