webguync Posted April 23, 2010 Share Posted April 23, 2010 I am trying to use in_array to set one class or another in CSS, but it's not working as expected. Here is my code <?php $wrong = array(4,10,17); $answers = array("4","5","10","12","17"); foreach($answers as $key => $value){ if (in_array($key, $wrong)){ $class = "wrong"; } else { $class="correct"; } echo "<td class=\"$class\">$value</td>"; ?> this outputs... <td class="correct">4</td><td class="correct">5</td><td class="correct">10</td><td class="correct">12</td><td class="wrong">17</td> but s/b <td class="wrong">4</td><td class="correct">5</td><td class="wrong">10</td><td class="correct">12</td><td class="wrong">17</td> any ideas what's up? Link to comment https://forums.phpfreaks.com/topic/199549-in_array-not-working-as-expected/ Share on other sites More sharing options...
mattal999 Posted April 23, 2010 Share Posted April 23, 2010 You're comparing the indexes, not the values. Should be: <?php $wrong = array(4, 10, 17); $answers = array(4, 5, 10, 12, 17); foreach($answers as $value){ if (in_array($value, $wrong)){ $class = "wrong"; } else { $class = "correct"; } echo "<td class=\"$class\">$value</td>"; } ?> Link to comment https://forums.phpfreaks.com/topic/199549-in_array-not-working-as-expected/#findComment-1047388 Share on other sites More sharing options...
webguync Posted April 23, 2010 Author Share Posted April 23, 2010 great, thanks! Link to comment https://forums.phpfreaks.com/topic/199549-in_array-not-working-as-expected/#findComment-1047396 Share on other sites More sharing options...
webguync Posted April 25, 2010 Author Share Posted April 25, 2010 I need to work this a bit. Instead of using numbers I need to use letters. So far what I have doesn't work. Every result displays as class="correct". Basicly, what I need is to compare the array $responses to $correct_responses and if they match up display class="correct", of they don't match up class="wrong". Can I still use in_array? <?php $correct_responses = array(c, d, e, a, a,b); $responses = array(c, b, a, a, b,b); foreach($correct_responses as $value){ if (in_array($value, $responses)){ $class = "wrong"; } else { $class = "correct"; } echo "<td class=\"$class\">$value</td>"; } ?> also $responses is being pulled from a database, not entered manually. $correct_resp is a constant and can be typed in. Link to comment https://forums.phpfreaks.com/topic/199549-in_array-not-working-as-expected/#findComment-1048259 Share on other sites More sharing options...
de.monkeyz Posted April 25, 2010 Share Posted April 25, 2010 You can use an array yes to compare strings (you need quotes around them). But since you're using only a few values that repeat. in_array won't work, you'll want to compare the ids exactly: <?php $correct_responses = array('c', 'd', 'e', 'a', 'a', 'b'); $responses = array('c', 'b', 'a', 'a', 'b', 'b'); foreach($correct_responses $key => $value){ if ($value != $responses[$key]){ $class = "wrong"; } else { $class = "correct"; } echo "<td class=\"$class\">$value</td>"; } ?> That will compare them in order, whereas in_array would find anything that matched. You also needed to reverse your if, since now you're working with correct responses, and not wrong ones. Link to comment https://forums.phpfreaks.com/topic/199549-in_array-not-working-as-expected/#findComment-1048261 Share on other sites More sharing options...
webguync Posted April 26, 2010 Author Share Posted April 26, 2010 thanks, not sure what is meant by reversing the if though. Also, is there a syntax error in the code provided? Link to comment https://forums.phpfreaks.com/topic/199549-in_array-not-working-as-expected/#findComment-1048273 Share on other sites More sharing options...
de.monkeyz Posted April 26, 2010 Share Posted April 26, 2010 err yeah, I forgot the 'as' keyword in the foreach. By reversing I mean the if is now checking if it is NOT equal, instead of equal. Link to comment https://forums.phpfreaks.com/topic/199549-in_array-not-working-as-expected/#findComment-1048276 Share on other sites More sharing options...
webguync Posted April 26, 2010 Author Share Posted April 26, 2010 ok just to verify s/b <?php $correct_responses = array('c', 'd', 'e', 'a', 'a', 'b'); $responses = array('c', 'b', 'a', 'a', 'b', 'b'); foreach($correct_responses as $key => $value){ if ($value == $responses[$key]){ $class = "wrong"; } else { $class = "correct"; } echo "<td class=\"$class\">$value</td>"; } ?> [ Link to comment https://forums.phpfreaks.com/topic/199549-in_array-not-working-as-expected/#findComment-1048277 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.