Check if a value is present in an array

2

I have a table in my database with multiple numbers. I get the numbers from the database, I save in array , and I make echo in a table in PHP:

echo "<table class='CSSTableGenerator'>";

echo "<tr>";

echo "<th>array</th>";

echo "<th>1</th>";
echo "<th>2</th>";
echo "<th>3</th>";
echo "<th>4</th>";
echo "<th>5</th>";
echo "<th>6</th>";
echo "<th>7</th>";
echo "<th>8</th>";
echo "<th>9</th>";
echo "<th>10</th>";
echo "<th>11</th>";
echo "<th>12</th>";
echo "<th>13</th>";
echo "<th>14</th>";
echo "<th>15</th>";
echo "<th>16</th>";
echo "<th>17</th>";
echo "<th>18</th>";
echo "<th>19</th>";
echo "<th>20</th>";
echo "<th>21</th>";
echo "<th>22</th>";
echo "<th>23</th>";
echo "<th>24</th>";
echo "<th>25</th>";
echo "<th>26</th>";
echo "<th>27</th>";
echo "<th>28</th>";
echo "<th>29</th>";
echo "<th>30</th>";

echo "</tr>";

while ($produto = mysql_fetch_array($produtos)) {
    $vetor=array($produto['id'],$produto['n1'],$produto['n2'],$produto['n3'],$produto['n4'],$produto['n5'],$produto['e1'],$produto['e2']);

    echo "<tr>";

    echo "<td>".$vetor[1]."&nbsp";
    echo $vetor[2]."&nbsp";
    echo $vetor[3]."&nbsp";
    echo $vetor[4]."&nbsp";
    echo $vetor[5]."&nbsp";
    echo $vetor[6]."&nbsp";
    echo $vetor[7] . "</td>&nbsp";


    for ($i = 1; $i <= 30; $i++) {
        echo '<td class="'.(($vetor[1]==$i) ? 'paint_me_green' : 'paint_me_red').'"></td>';

    }

    echo "</tr>";

}

echo "</table>";

?>

The numbers ( array ) are written in the <th>array<th> column and in other <th>1 to 50 <th> I'd like to check if the numbers contained in one of my arrays ex: array 10 12 23 10 223. If the number 10 exists in this array a <th>10</th> it will be green in the line of that array , otherwise red.

So far as you can see in my code, within while and I put a for

for ($i = 1; $i <= 50; $i++) {
    echo '<td class="'.(($vetor[1]==$i) ? 'paint_me_green' : 'paint_me_red').'"></td>';
}

but this way it is only checking if:

  • position [1] == 1 it paints green if it does not paint red.
  • position [1] == 2 it paints green if it does not paint red.
  • ...
  • position [1] == 23 It paints green if it does not paint red.

And it works this way ... it's what I want. But I want to do the same for the remaining positions besides the [1] position.

The% w / w I'm using should not be the most accurate way of looking at the remaining positions.

It would be something of this kind that I would need:

for ($i = 1; $i <= 50; $i++) {
    echo '<td class="'.(($vetor[1]==$i) and ($vetor[2]==$i) and ($vetor[3]==$i) and ($vetor[4]==$i) and and ($vetor[5]==$i) and ($vetor[7]==$i) ? 'paint_me_green' : 'paint_me_red').'"></td>';
}

They spoke to me in for I just can not apply.

    
asked by anonymous 08.08.2015 / 17:28

1 answer

4

You need the in_array() function.

for ($i = 1; $i <= 50; $i++) {
    echo '<td class="'.(in_array($i, $vetor) ? 'paint_me_green' : 'paint_me_red').'"></td>';
}

See running on ideone .

    
08.08.2015 / 17:44