如何将 2 个参数传递给 php 中的 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";

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

Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in C:\xampp\htdocs\exam.php on line 359

改用回调函数

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);
});