0

I am trying to run an array_search on a foreach loop

$i=0;
foreach(findCustomAlerts($customerid) as $key=>$value){ 
   echo "ID of cat : ".$rifdid[$i++] = $value['Catid']."<br>";
   echo "Visits required per day : ".$visitsneeded= $value['Visits required']."<br>";
}

echo "<pre>";
print_r($rifdid);
echo "</pre>";

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

}
}

The data for the first for each loop is as follows:

Array ( [0] => Array ( [Alertid] => 4 [Catid] => 5609 [Visits required] => 6 ) [1] => Array ( [Alertid] => 5 [Catid] => 23641 [Visits required] => 5 ) )

The data for the second foreach is as follows:

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

I want to compare the two arrays, and where the rfid id has not reached the required number of visits for that day I need to perform another action.

At the moment when I run the code, it is only picking up that the RFID id 23641 hasnt had enough visits that day, when it should be picking up that both rfids have not been seen enough

This is the current output: ID of cat : 5609 Visits required per day : 6 ID of cat : 23641 Visits required per day : 5 23641 has not visited enough

It also needs to output 5609 has not visited enough.

2
  • 1
    your question is not clear, post all of your input and your expected output. Commented Oct 3, 2020 at 17:04
  • Just edited to add additional detals Commented Oct 3, 2020 at 17:15

1 Answer 1

1

Take a look at the following code:

Your first array:

$first_arr = Array ( 
        Array ( 
            'Alertid' => 4,
            'Catid' => 5609,
            'Visits_required' => 6 
        ),
        Array ( 
            'Alertid' => 5,
            'Catid' => 23641,
            'Visits_required' => 5
        )
    );

Your second array;

$second_arr = Array ( 
        'rfid_id' =>  Array ( 
            23641 => 1,
            5609 => 3
        ) 
    );

First make a loop in your first array and then make loop in your second array. Match catid and check required visit

foreach( $first_arr as $item ){

    foreach( $second_arr as $item2 ){

        foreach( $item2 as $key => $val ){

            if( $key == $item['Catid'] && $val < $item['Visits_required']){
                echo $key .' -- not enough visited <br>....<br>';
            }
            elseif( $key == $item['Catid'] && $val > $item['Visits_required']){
                echo $key . ' -- visited enough <br>....<br>';
            }

        }

    }

}

It will output like:

5609 -- not enough visited
....
23641 -- not enough visited
....

I have added some dots and br to make the output in separate line. You can use the code what needed for you.

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.