Print highlighted matrix-specific values

2

In my studies here with arrays I'm trying to highlight only the odd values (leave bold), but I'm not understanding how to do this. Below is what I did, but it prints both bold and normal numbers.

 <?php
 $matriz = array(
     array(50, 35, 44),
     array(25, 11, 32),
     array(53, 95, 78)
 );
 foreach ($matriz as $v1) {
     foreach ($v1 as $v2) {
         echo $v2, ' '; // imprime todos valores com espaço
         if ($v2 & 1) { // se for impar
             echo '<b>', $v2, '</b>';
         }
     }
     echo '<br/>';
 }
 ?>
    
asked by anonymous 13.03.2014 / 14:47

2 answers

2

Your problem was a simple logic error when using two echo to print the items, the correct one would be as follows:

    foreach ($matriz as $v1){
       foreach ($v1 as $v2){
          // eu removi o echo daqui
           if ($v2 & 1) { // se for impar
               echo '<b>', $v2, ' </b>';
           } else {
              echo $v2,' '; // e coloquei ele aqui
           }
       }
       echo '<br/>';
   }

What was wrong was that you first printed all the items in the array, and then created a if() by printing a second time items that were odd, this time in bold type. That's why these items were duplicated.

    
13.03.2014 / 14:50
2

To find out if a number is odd or odd, use the following:

Pair:

$numero % 2 == 0

Odd:

$numero % 2 == 1

So it becomes clear what you mean, in my opinion.

Furthermore, it would not use <b> and yes <strong> .

    
13.03.2014 / 14:50