PHP如何在多维数组中改变变量


PHP how to change var within multidimensional array

我试图改变一个多维数组字段的值。请注意,我不是PHP专家,我仍在学习。

我使用下面的结构

array(
      array(ID+value, GRANTED+value)
)

我使用以下代码:

// accept in session
foreach ($_SESSION['chatrequests'] as $request) {
    error_log("accepting chat request with id " . $request['ID'] . "  and the incoming  request is " . $requestid);
    if ($request['ID'] == $requestid) {
        error_log("matching request ids found. so granting");
        $request['GRANTED'] = "Y";
        error_log("did the granting work ???? ->" . $request['GRANTED']);
    }
}

日志显示$request[' granting ']的值被设置为y。

因此,在对var进行假定的编辑之后,我读出数组以查看编辑是否成功。

foreach ($_SESSION['chatrequests'] as $request) {
    error_log("reading chat request with id " . $request['ID'] . "  and the incoming request is " . $requestid);
    if ($request['ID'] == $requestid) {
        error_log("matching request ids found. was it granted " . $request['GRANTED']);
    }
}

但是$request[' granting ']仍然被设置为n

问题是我该怎么做?哪一种模式可以做到这一点?

这很正常!$request[' granting '] = "Y";, $_SESSION数组未被修改。你必须这样做:

// accept in session
foreach ($_SESSION['chatrequests'] as $key => $request) {
    error_log("accepting chat request with id " . $request['ID'] . "  and the incoming  request is " . $requestid);
    if ($request['ID'] == $requestid) {
        error_log("matching request ids found. so granting");
        $_SESSION['chatrequests'][$key]['GRANTED'] = "Y";
        error_log("did the granting work ???? ->" . $request['GRANTED']);
    }
}