dongqiulei6805 2011-10-20 22:37
浏览 20
已采纳

动态创建PHP数组

I am having the worst time trying to get this to work. In the following code, I am gathering data from a database query and trying to build a muti-dimensional array object that will keep totals and tally up some information in a specific way. The problem is that instead of getting a value that is incrementing as it should, the value seems to be suffering from the last value it was assigned problem. Here is the code:

        $REVIEWS      = array();
        $USER_REVIEWS = array();
        $USER_IMGREVS = array();


        pseudo-code: loop here which iterates over the DB results creating
        $date - which is into this function as its called for each day of month
        $p1user - which is one of the users (there are 3) 'levels' of users
        $hr - is the hour which is built from the transaction's timestamp

        $hr     = date('H', $row['P1TIMESTAMP']);
        $p1user = $row['P1USER'];

        $REVIEWS[$date] += 1;
        $USER_REVIEWS[$date][$p1user][$hr] += 1;
        $USER_IMGREVS[$date][$p1user][$hr] += $row['F5'];

        print "PASS1<br/>
";
        print "Value of Total Reviews: [".$REVIEWS[$date]."]<br/>
";
        print "Value of User Reviews: [".$USER_REVIEWS[$date][$p1user][$hr]."]<br/>
";
        print "Value of Reviewed Images: [".$USER_IMGREVS[$date][$p1user][$hr]."]<br/>
";
        print "<br/><br/>
";

So - the 'total reviews' increments by one, as it should, for each time i print this. SO far so good. The next two arrays will only print the last values they were assigned, and will not be added together like they should. Why not? I have attempted to do this another way by literally creating the arrays one by one and assigning them in whole to the array containing them - but that also does not seem to work. Any insights?

  • 写回答

3条回答 默认 最新

  • dongren4758 2011-10-20 23:32
    关注

    i don't know how you initilize your array, maybe this will help:

    // replace this 2 lines:
    $USER_REVIEWS[$date][$p1user][$hr] += 1;
    $USER_IMGREVS[$date][$p1user][$hr] += $row['F5'];
    
    // with this code:
    if (!isset($USER_REVIEWS[$date]))
        $USER_REVIEWS[$date] = array();
    if (!isset($USER_REVIEWS[$date][$p1user]))
        $USER_REVIEWS[$date][$p1user] = array();
    if (!isset($USER_REVIEWS[$date][$p1user][$hr]))
        $USER_REVIEWS[$date][$p1user][$hr] = 0;
    $USER_REVIEWS[$date][$p1user][$hr] += 1;
    
    if (!isset($USER_IMGREVS[$date]))
        $USER_IMGREVS[$date] = array();
    if (!isset($USER_IMGREVS[$date][$p1user]))
        $USER_IMGREVS[$date][$p1user] = array();
    if (!isset($USER_IMGREVS[$date][$p1user][$hr]))
        $USER_IMGREVS[$date][$p1user][$hr] = 0;
    $USER_IMGREVS[$date][$p1user][$hr] += $row['F5'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大