Jump to content

in_array not working as expected


webguync

Recommended Posts

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

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>";
}

?>

 

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.

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.

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>";
}

?>
[

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.