0

I am trying to count the instances of rfid from one array to insert into another array so I can use this data to create charts.

At the moment my code is as follows

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

Which outputs as follows :

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
)

Ideally, I would like to achieve the following :

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

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

)

or something to that effect, where I can look at the date, rfid codes and the time each was read on that date.

1 Answer 1

1

You simply missing the path in the assignment loop.

Same goes for rfid and date so I just explain on rfid. You check the isset on one path but is not exist set on another - this cause the path to never exist - that why you keep getting 1's.

Look at:

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

Notice $a[$event['rfid']] is NOT the same as $a['rfid_id'][$event['rfid']].

You should change the if statement to be: if (isset($a['rfid_id'][$event['rfid']))

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.