Kalland Posted April 29, 2009 Share Posted April 29, 2009 Hi, I'm having some trouble looping through an array. The inner loop loops through the array and checks if a seat is reserved, but it only returns true for the last row. Seats 1 and 5 are supposed to have the class reserved, but it's only applied to seat 5. If I try to echo out the $tmp variable it shows 1 and 5 between every row. The outer loop creates the seats. What could be the problem here? <?php error_reporting(E_ALL); $list[] = array('1', 'test'); $list[] = array('5', 'test2'); $rows = count($list); /** Create seats */ for ($i = 1; $i <= 80; $i++) { for ($j = 0; $j < $rows; $j++) { $tmp = $list[$j][0]; echo $tmp; /** Check if seat is already reserved */ $class = ($i == $tmp) ? 'reserved' : 'available'; } echo "<div class='$class'>$i</div>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/156101-solved-problem-with-looping-through-an-array/ Share on other sites More sharing options...
sasa Posted April 29, 2009 Share Posted April 29, 2009 <?php error_reporting(E_ALL); $list[] = array('1', 'test'); $list[] = array('5', 'test2'); $rows = count($list); //create array of reserved seats for ($j = 0; $j < $rows; $j++) { $tmp[] = $list[$j][0]; } /** Create seats */ for ($i = 1; $i <= 80; $i++) { $class = (in_array($i;$tmp)) ? 'reserved' : 'available'; echo "<div class='$class'>$i</div>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/156101-solved-problem-with-looping-through-an-array/#findComment-821742 Share on other sites More sharing options...
Kalland Posted April 29, 2009 Author Share Posted April 29, 2009 That worked, thank you ! Quote Link to comment https://forums.phpfreaks.com/topic/156101-solved-problem-with-looping-through-an-array/#findComment-821744 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.