我试图修改多维数组的每个数组元素中第一个子元素的内容,但是,如果我通过引用传递所述元素,我会得到一个错误“&”不是预期的。我是否需要抛弃foreach($ arr as list())语法并恢复为循环标准并使用计数器变量进行访问?
foreach ($transactions as list(&$student, &$tyID)) {
$query = "SELECT $column
FROM student
WHERE stUserID=$student";
if ($stmt = mysqli_prepare($link, $query)) {
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $userHID);
while (mysqli_stmt_fetch($stmt)) {
$student = $userHID;
$tyID = $transType[$tyID];
}
mysqli_stmt_close($stmt);
}
}
编辑:更新了代码,我实际上需要修改Transactions数组中每个数组的前两个子元素。
答案 0 :(得分:1)
为什么你不能这样做?假设密钥与列表变量相同
foreach ($transactions as &$transaction) {
$query = "SELECT {$column}
FROM student
WHERE stUserID={$transaction['student']}";
if ($stmt = mysqli_prepare($link, $query)) {
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $userHID);
while (mysqli_stmt_fetch($stmt)) {
$transaction['student'] = $userHID;
$transaction['tyID'] = $transType[$transaction['tyID']];
}
mysqli_stmt_close($stmt);
}
}