首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >foreach循环PHP中值的递增计数

foreach循环PHP中值的递增计数
EN

Stack Overflow用户
提问于 2020-09-28 19:38:23
回答 1查看 135关注 0票数 0

我正在尝试计算一个数组中rfid的实例数,以便插入到另一个数组中,这样我就可以使用这些数据来创建图表。

目前,我的代码如下

代码语言:javascript
复制
if ($message == "Broken" && $message_type == "BreakBeam" && $previous==$cat_id) {
            
            $total = 1;
            
            $splitTimeStamp = explode(" ",$eventtime);
            $date = $splitTimeStamp[0];
            $time = $splitTimeStamp[1];
        
            $events[$c]['date'] = $date;
            $events[$c]['device'] = $device;
            $events[$c]['time']= $time;
            $events[$c]['rfid']= $previous;
            $events[$c]['count']=$total;
            $c++;
        }   
        }  


        $a = array();
        $i=0;

        foreach($events as $event){
          if(isset($a[$event['rfid']])){
            $a['rfid_id'][$event['rfid']]++;
          }else{
            $a['rfid_id'][$event['rfid']]=1;
          }
          if(isset($a[$event['date']])){
            $a['dateinsert'][$event['date']]++;
          }else{
            $a['dateinsert'][$event['date']] =1;        
           }
        }



    $rfid = array();
    // those are the ones we're going to put in!

    foreach($a as $key => $count) {
      foreach($count as $eventdetails['rfid_id'] => $event){
      // so $key is the [rfid] key of the entry and $count is the count (all 1's?) in it
    if (isset($rfid[$key])) {
      $rfid[$key]+=$event;
    }
    else {
      $rfid[$key]=$event;
    }
    }
  }

输出结果如下:

代码语言:javascript
复制
Array
(
    [0] => Array
        (
            [date] => 2020-09-17
            [device] => 2
            [time] => 15:51:37
            [rfid] => 23641
            [count] => 1
        )

    [1] => Array
        (
            [date] => 2020-09-17
            [device] => 2
            [time] => 15:52:20
            [rfid] => 5609
            [count] => 1
        )

    [2] => Array
        (
            [date] => 2020-09-17
            [device] => 2
            [time] => 15:53:23
            [rfid] => 5609
            [count] => 1
        )

    [3] => Array
        (
            [date] => 2020-09-17
            [device] => 2
            [time] => 16:02:44
            [rfid] => 5609
            [count] => 1
        )

)
Array
(
    [rfid_id] => Array
        (
            [23641] => 1
            [5609] => 1
        )

    [dateinsert] => Array
        (
            [2020-09-17] => 1
        )

)
Array
(
    [rfid_id] => 2
    [dateinsert] => 1
)

理想情况下,我希望实现以下目标:

代码语言:javascript
复制
Array
(
    [rfid_id] => Array
        (
            [23641] => 1
            [5609] => 3
        )

    [dateinsert] => Array
        (
            [2020-09-17] => 1
        )

)

或者类似的东西,我可以查看日期,rfid代码和在该日期读取每个代码的时间。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-29 16:13:02

你只是在赋值循环中遗漏了路径。

rfiddate也是如此,所以我只是在rfid上解释一下。你在一条路径上检查isset,但在另一条路径上设置为不存在-这会导致该路径永远不存在-这就是为什么你总是得到1。

请看:

代码语言:javascript
复制
foreach($events as $event){
  if(isset($a[$event['rfid']])){
    $a['rfid_id'][$event['rfid']]++;
  } else {
    $a['rfid_id'][$event['rfid']]=1;
  }
}

请注意,$a[$event['rfid']]$a['rfid_id'][$event['rfid']]不同。

您应该将if语句更改为:if (isset($a['rfid_id'][$event['rfid']))

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64101463

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档