Comparing PHP imprint array

Asked

Viewed 49 times

-1

I have two array one that limits me and the other that comes with the database information.

$limite = array("06","07","08","09","10","11","12");
$mes = array("09","10","11","12");

it generates a table row. When compared I want you to print the value of $mes when you have it in the same limit index. And when you don’t find the value comes out zero. I even made but it prints the empty values at the end and not at the beginning as I want

<table>
   <tr>
       <td></td>
       <td></td>
       <td>09</td>
       <td>10</td>
       <td>11</td>
       <td>12</td>
   </tr>

here is the code I made but only prints at the end instead of the beginning

 for($a=0;$a<count($semestre2);$a++){
     $mes = array_search($mes[$a], $limite);
     if($mes){
         echo '<td>'.$dateRange[$a].'_p'].'</td>';             
     }else{
         echo '<td></td>';           
     }
 }
  • Will the data always return so ordered? For example: limit(06,07,08...) and ordered month tbm, type: (09,10,11)? At no time can this happen, ex: array("06","09","10","11","12"), see which way from the bank, (06,09...)

  • Who is the $semestre2? Because you count it to assemble the cells of the table.

1 answer

0


If the data always returns sorted, as defined in your question and that the list of boundary is responsible for assembling the cells of the table, for its logic of 0. It makes me think that at no time this will happen:

$limite = array("11","12");
$mes = array("09","10","11","12");

For that would be the result:

11  12

So with that in mind, list of boundary for the assembly and comparison. It would solve so:

$limite = array("06","07","08","09","10","11","12");
$mes = array("09","10","11","12");
    
echo '<table><tr>';

foreach($limite as $chave => $valor):
    if(in_array($valor, $mes)):
        echo "<td>$valor</td>";
    else:
        echo "<td>0</td>";
    endif;
endforeach;

echo '</tr></table>';

Exit:

0   0   0   09  10  11  12

Now if at any time it comes to pass:

$limite = array("04","05","06","07","08","09","10","11","12");
$mes = array("05","08","09","10","11","12");

You would have it here as a result:

0   05  0   0   08  09  10  11  12

Browser other questions tagged

You are not signed in. Login or sign up in order to post.