samedi 3 octobre 2020

If Statement with Foreach not iterating through foreach

I am having an issue with my code - fairly new to PHP so any pointers would be appreciated.

I have the following code

foreach(findCustomAlerts($customerid) as $key=>$value){

  echo "ID : ".$rifdid = $value['Catid']."<br>";
  echo "Visits required per day : ".$visitsneeded= $value['Visits required']."<br>";
  
}

foreach(getTodaysVisits($device_id) as $rfid){
    foreach($rfid as $key=> $read){
 
     if($key ==$rifdid && $read < $visitsneeded) {  
        echo"<br>". $key." has not visited enough";
       
     }
    
  }

}

But it is not iterating through the foreach - it is only picking up the first values and then stopping :

ID: 1005
Visits required per day : 3
ID : 2089
Visits required per day : 5

2089 has not visited enough

This is the array that the foreach should be iterating through:

Array
(
    [rfid_id] => Array
        (
            [2089] => 1
            [1005] => 3
        )

)

I have tried putting a variable in to count the iterations like so :

$i = 0;

foreach(getTodaysVisits($device_id) as $rfid){
    foreach($rfid as $key[$i]=> $read){
 
     if($key[$i] ==$rifdid && $read < $visitsneeded) {  
        echo"<br>". $key[$i]." has not visited enough";
       $i++;
     }
    
  }

}

But it gives me the following errors :

Warning: Cannot use a scalar value as an array 
Notice: Trying to access array offset on value of type int in

So not sure what the best course of action is, any advice greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire