PHP如何在多维数组中更改var

时间:2014-08-10 10:11:59

标签: php arrays multidimensional-array

我正在尝试更改a中字段的值。多维数组。请注意,我不是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 ['GRANTED']值设置为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 ['GRANTED']仍然设置为N。

问题是我该怎么做?哪种模式可以实现这个目标?

1 个答案:

答案 0 :(得分:0)

这很正常!你什么时候要求[' GRANTED'] =" Y&#34 ;; ,$ _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']);

    }
}