Php 关联数组错误

Php 关联数组错误,php,mysql,associative-array,Php,Mysql,Associative Array,我不想从MySQL创建JSON php输出,数据库至少包含以下内容 用户名、日期时间、授权时间 我的逻辑包括$key,它是基于“key”的用户名和ASSOC数组,但是 用户名的末尾包含两个额外字符_1、_2、_3等 $rows = array(); $acctsessiontime=0; while ($row = mysql_fetch_assoc($result)) { $key=substr($row['username'], 0, -2); $acctse

我不想从MySQL创建JSON php输出,数据库至少包含以下内容 用户名、日期时间、授权时间

我的逻辑包括$key,它是基于“key”的用户名和ASSOC数组,但是 用户名的末尾包含两个额外字符_1、_2、_3等

$rows = array();
$acctsessiontime=0;
while ($row = mysql_fetch_assoc($result)) {
        $key=substr($row['username'], 0, -2);
        $acctsessiontime+=round($row['acctsessiontime']/60*.20,2);
        $acctsessiontimeby[$key]+=$acctsessiontime;
        $datetime=$row['datetime'];
        $rows[$key][] = "[$datetime,$acctsessiontimeby[$key]]";
        $rows['All'][]= "[$datetime,$acctsessiontime]";
}
php错误:
注意:第77行\xampp\htdocs\intercall\scripts\server\u processing2.php中未定义的偏移量:0

价值观:

$row['username'] /// output user1_1
$key // output user1
$acctsessiontime // output the $$$ calculated a 60seg in a min and 0.20$ by min
$acctsessiontimeby[$key]+=round($row['acctsessiontime']/60*.20,2);

正如
Leonardo
所说,您必须首先将
$acctsessiontimeby
定义为一个数组。 但这还不是全部<代码>$acctsessiontimeby[$key]+=运算符引用“$acctsessiontimeby[$key]”中的值。当密钥不存在时,PHP抛出错误

首先,您应该检查密钥是否存在,而不是:

$acctsessiontimeby[$key]+=round($row['acctsessiontime']/60*.20,2);
你应该写:

$acctsessiontimeby = array(); // before while loop

// ...

if( isset( $acctsessiontimeby[$key] ) ){
    $acctsessiontimeby[$key]+=round($row['acctsessiontime']/60*.20,2);
} else {
    $acctsessiontimeby[$key]=round($row['acctsessiontime']/60*.20,2);
}

您在哪里将$acctsessiontimeby定义为数组?感谢您的回答,只需添加莱昂纳多声明$acctsessiontimeby=array()//在此之前和现在,我不需要我的解决方法…调用相同的过程4次XP
$acctsessiontimeby = array(); // before while loop

// ...

if( isset( $acctsessiontimeby[$key] ) ){
    $acctsessiontimeby[$key]+=round($row['acctsessiontime']/60*.20,2);
} else {
    $acctsessiontimeby[$key]=round($row['acctsessiontime']/60*.20,2);
}