Emmet89 Posted October 3, 2020 Share Posted October 3, 2020 (edited) 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 October 3, 2020 by Barand code block/indents Quote Link to comment https://forums.phpfreaks.com/topic/311562-issue-with-foreach-with-if-statement/ Share on other sites More sharing options...
gw1500se Posted October 3, 2020 Share Posted October 3, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/311562-issue-with-foreach-with-if-statement/#findComment-1581714 Share on other sites More sharing options...
Emmet89 Posted October 3, 2020 Author Share Posted October 3, 2020 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? Quote Link to comment https://forums.phpfreaks.com/topic/311562-issue-with-foreach-with-if-statement/#findComment-1581715 Share on other sites More sharing options...
gw1500se Posted October 3, 2020 Share Posted October 3, 2020 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) { Quote Link to comment https://forums.phpfreaks.com/topic/311562-issue-with-foreach-with-if-statement/#findComment-1581716 Share on other sites More sharing options...
Emmet89 Posted October 3, 2020 Author Share Posted October 3, 2020 (edited) 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 October 3, 2020 by Emmet89 Quote Link to comment https://forums.phpfreaks.com/topic/311562-issue-with-foreach-with-if-statement/#findComment-1581717 Share on other sites More sharing options...
gw1500se Posted October 3, 2020 Share Posted October 3, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/311562-issue-with-foreach-with-if-statement/#findComment-1581718 Share on other sites More sharing options...
Emmet89 Posted October 3, 2020 Author Share Posted October 3, 2020 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? Quote Link to comment https://forums.phpfreaks.com/topic/311562-issue-with-foreach-with-if-statement/#findComment-1581719 Share on other sites More sharing options...
gw1500se Posted October 3, 2020 Share Posted October 3, 2020 How do you know it is not going to the next value? You don't echo anything if there is a match. Quote Link to comment https://forums.phpfreaks.com/topic/311562-issue-with-foreach-with-if-statement/#findComment-1581720 Share on other sites More sharing options...
Emmet89 Posted October 3, 2020 Author Share Posted October 3, 2020 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? Quote Link to comment https://forums.phpfreaks.com/topic/311562-issue-with-foreach-with-if-statement/#findComment-1581721 Share on other sites More sharing options...
gw1500se Posted October 3, 2020 Share Posted October 3, 2020 And the other (5609) apparently was visited enough. Quote Link to comment https://forums.phpfreaks.com/topic/311562-issue-with-foreach-with-if-statement/#findComment-1581722 Share on other sites More sharing options...
Emmet89 Posted October 3, 2020 Author Share Posted October 3, 2020 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 Quote Link to comment https://forums.phpfreaks.com/topic/311562-issue-with-foreach-with-if-statement/#findComment-1581723 Share on other sites More sharing options...
gw1500se Posted October 3, 2020 Share Posted October 3, 2020 You need to echo $visitsneeded and $read to see what they really are. Quote Link to comment https://forums.phpfreaks.com/topic/311562-issue-with-foreach-with-if-statement/#findComment-1581724 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.