【问题标题】:Increment count for value in foreach loop PHPforeach循环PHP中值的递增计数
【发布时间】:2021-01-13 23:51:09
【问题描述】:

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

目前我的代码如下

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;
    }
    }
  }

输出如下:

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
)

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

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

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

)

或类似的东西,我可以在其中查看日期、rfid 代码以及每个在该日期被读取的时间。

【问题讨论】:

    标签: php arrays multidimensional-array associative-array


    【解决方案1】:

    你只是错过了赋值循环中的路径。

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

    看看:

    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']))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-12
      • 2022-01-03
      • 2015-05-19
      • 2019-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多