Jump to content

Recommended Posts

Hi I am new to PHP and I am having an issue with a foreach loop combined with an if statement, basically the if statement is getting the data from the first result in the foreach but not getting the second result, I have tried implement a count by using a variable to iterate but it isnt working: 

Code as follows:

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

    echo "ID of cat : ".$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";

        }

    }

}

Ouput is : 

ID of cat : 5609
Visits required per day : 3
ID of cat : 23641
Visits required per day : 5

Array ( [rfid_id] => Array ( [23641] => 1 [5609] => 3 ) ). -------> this is the array data 


23641 has not visited enough

-----------------------------------------------

How can I get the loop to iterate to the next value? 

 

 

 

Edited by Barand
code block/indents
Link to comment
https://forums.phpfreaks.com/topic/311562-issue-with-foreach-with-if-statement/
Share on other sites

First please use the code icon (<>) for you code and specify PHP. The formatter makes your code much easier to read.

The problem, I believe, is $rifdid. Notice that you only set that variable in the first foreach loop. So when you use it in the if block, it will always have the same value. That value is whatever it was set to on the last pass of the first foreach loop. It seems unlikely that is what you really want.

Sorry man! Didnt check the formatting rules before I posted - will be sure to do that next time. 

I can see now what you mean by the $rfidid - that is definately not what I need as I need to access each of the $rfidids that are in the list. 

How would the best way in going  about combining the two so I can gain access to both ids?

Use an array for $firdid In the first loop:

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

Then use that in the other loop:

i=0;
foreach($rfid as $key=> $read){
   if($key ==$rifdid[$i++] && $read < $visitsneeded) { 

This assumes $value will result in $rifdid being in the same order as $rfid. If it is not then you will need to search the array to see if that key is in it. See array_search.

if(array_search($key,$rifdid) && $read < $visitsneeded) {

 

hey thanks for the feeback 

 

I've implemented as follows : 

$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>";


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

}

}

 

But now instead of picking up the last item its only picking up the first item - have i implemented the code incorrectly?

Output : 

ID of cat : 5609
Visits required per day : 3
ID of cat : 23641
Visits required per day : 5
5609 has not visited enough

 

When I use the array_search its not picking up any of the items from the array at all. 

Edited by Emmet89

Yes but the order may not be what you expect. Do the following after the first loop to debug it

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

Then echo $key just before the 'if':

I am guessing the order is not what you expect so you may need to use the 2nd 'if' statement.

 

Yes thats exactly it, here is the output when i echo both out :

Array
(
    [0] => 5609

    [1] => 23641

)
23641
5609

But when I implement the Array_search it still isnt looping into the next value 

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

}
}

Ive no idea why it wouldnt be going into the next value?

Its giving me an output of : 

23641 has not visited enough

 

From this: 

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

Am I not using the array_search correctly?

Man your totally right! 

But, I just changed the requirements so 5609 hasnt hit enough visits and it still isnt working as expected!

ID of cat : 5609
Visits required per day : 6
ID of cat : 23641
Visits required per day : 5

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

23641 has not visited enough

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.