bluebyyou Posted June 25, 2007 Share Posted June 25, 2007 Can I use AND or && in a foreach loop, like this: foreach ($_POST['day'] as $day && $_POST['event'] as $event) { $query = "INSERT INTO event(year,day,event) VALUES('$_POST[year]','$day','$event')"; } Or is there some other way to do this? Link to comment https://forums.phpfreaks.com/topic/57017-solved-foreach-loop-with-logical-operator/ Share on other sites More sharing options...
btherl Posted June 25, 2007 Share Posted June 25, 2007 Unfortunately, no. Here are two alternate styles. # This one only works for arrays numbered consecutively from 0 $i = 0; $arr_len = count($arr); for ($i = 0; $i < $arr_len; $i++) { print $arr[$i]; print $arr2[$i]; } # This one works for anything reset($arr); reset($arr2); while (list($arr_key, $arr_val) = each($arr)) { list($arr2_key, $arr2_val) = each($arr2); } In both cases, the length of the loop is decided by the length of $arr, in the case of a mismatch in array lengths. Link to comment https://forums.phpfreaks.com/topic/57017-solved-foreach-loop-with-logical-operator/#findComment-281673 Share on other sites More sharing options...
bluebyyou Posted June 25, 2007 Author Share Posted June 25, 2007 Thank you that did it. Link to comment https://forums.phpfreaks.com/topic/57017-solved-foreach-loop-with-logical-operator/#findComment-281735 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.