unknown1 Posted November 4, 2009 Share Posted November 4, 2009 Hello all, I wanted to see if someone could give me the correct syntax for creating a foreach loop with multiple conditions. I have tried a few things but all result in error parse error. e.g. foreach(($rdate as $d)&& ($url as $u)){ } results in error... Quote Link to comment https://forums.phpfreaks.com/topic/180221-solved-multiple-conditions-in-a-foreach-loop/ Share on other sites More sharing options...
Gayner Posted November 4, 2009 Share Posted November 4, 2009 LOL What are the variables for these? Quote Link to comment https://forums.phpfreaks.com/topic/180221-solved-multiple-conditions-in-a-foreach-loop/#findComment-950722 Share on other sites More sharing options...
genericnumber1 Posted November 4, 2009 Share Posted November 4, 2009 Well, conditions wouldn't be the right word to use, because they're not really conditions. They're more like expressions. As far as I know, there are no ways to (linearly) loop through two arrays using only the foreach loop. That said, <?php while(list(,$rdate) = each($d) && list(,$url) = each($u)) { } ?> Would be comparable to your desired loop, although it's a bit difficult to read. There are cleaner ways of doing what you're suggesting. Quote Link to comment https://forums.phpfreaks.com/topic/180221-solved-multiple-conditions-in-a-foreach-loop/#findComment-950723 Share on other sites More sharing options...
unknown1 Posted November 4, 2009 Author Share Posted November 4, 2009 Well, conditions wouldn't be the right word to use, because they're not really conditions. They're more like expressions. As far as I know, there are no ways to (linearly) loop through two arrays using only the foreach loop. That said, <?php while(list(,$rdate) = each($d) && list(,$url) = each($u)) { } ?> Would be comparable to your desired loop, although it's a bit difficult to read. There are cleaner ways of doing what you're suggesting. I assume the code above and will do the same thing as a foreach loop? Quote Link to comment https://forums.phpfreaks.com/topic/180221-solved-multiple-conditions-in-a-foreach-loop/#findComment-950728 Share on other sites More sharing options...
genericnumber1 Posted November 4, 2009 Share Posted November 4, 2009 Yes, it will loop through both arrays linearly, terminating execution when one of the arrays runs out of data members. Note this means that if one array (let's call it L) has more elements than the other array (let's call it S), the loop will only iterate through count(S) elements of L. Quote Link to comment https://forums.phpfreaks.com/topic/180221-solved-multiple-conditions-in-a-foreach-loop/#findComment-950730 Share on other sites More sharing options...
unknown1 Posted November 4, 2009 Author Share Posted November 4, 2009 Yes, it will loop through both arrays linearly, terminating execution when one of the arrays runs out of data members. Note this means that if one array (let's call it L) has more elements than the other array (let's call it S), the loop will only iterate through count(S) elements of L. Okay, this is my script... and when I run it I get an error Warning: Variable passed to each() is not an array or object in C:\xampp\htdocs\2\test.php on line 28 and script fails. And yes both $u and $r have the same amount of elements <?php include('config.php'); $sql_rdate="SELECT * FROM table_tmp WHERE status='1'"; $rs_dt=mysql_query($sql_rdate) or die (mysql_error()); $sql_url="SELECT * FROM table_tmp WHERE status='1'"; $rs_url=mysql_query($sql_url) or die (mysql_error()); //while($r= mysql_fetch_assoc($rs_url)){ //$url[]=$r; //} while($row= mysql_fetch_assoc($rs_dt)&& $r= mysql_fetch_assoc($rs_url)){ $rdate[]=$row; $url[]=$r; } while(list(,$rdate) = each($d) && list(,$url) = each($u)) { $u['Url'] = trim($u['Url']); //Remove whitespace in case there is some. $d['rDate'] = trim($d['rDate']); $expire=date('Y-m-d',strtotime($d['rDate'].' + 90 days')); echo "$expire $u[url]<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/180221-solved-multiple-conditions-in-a-foreach-loop/#findComment-950732 Share on other sites More sharing options...
genericnumber1 Posted November 4, 2009 Share Posted November 4, 2009 I'm sorry, I just skimmed your original code, not spending much attention. You might want to look into list() and each() to see what my mistake was, but the short fix is to swap $rdate with $d and $url with $u. That said, why have two seperate loops? Just place the logic in this loop we're creating in the while loop that does mysql_fetch_assoc to begin with. Quote Link to comment https://forums.phpfreaks.com/topic/180221-solved-multiple-conditions-in-a-foreach-loop/#findComment-950733 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.