vendredi 15 juillet 2016

PHP Using an if statement inside a foreach loop

I have an array which is the result of a MySQLi query.

The array code is:

while ($row_Active = mysqli_fetch_assoc($SeqActive)) {
    $Seq[] = $row_Active;
} 

the results of the query:

Array ( [0] => Array ( [Status] => A [SeqID1] => 1 [SeqID2] => 1 [SeqID3] => [SeqID4] => [SeqID5] => [SeqID6] => [SeqID7] => [SeqID8] => [SeqID9] => [SeqID10] => [SeqID11] => [SeqID12] => [SeqID13] => [SeqID14] => [SeqID15] => [SeqID16] => [SeqID17] => [SeqID18] => [SeqID19] => [SeqID20] => [SeqID21] => [SeqID22] => [SeqID23] => [SeqID24] => [SeqID25] => [SeqID26] => [SeqID27] => [SeqID28] => [SeqID29] => [SeqID30] => [SeqID31] => [SeqID32] => [SeqID33] => [SeqID34] => [SeqID35] => [SeqID36] => [SeqID37] => [SeqID38] => [SeqID39] => [SeqID40] => [SeqID41] => [SeqID42] => [SeqID43] => [SeqID44] => [SeqID45] => [SeqID46] => [SeqID47] => ) )

If I echo out the array it has the correct content so I know trhat works.

Goal:

I need to loop through the array (using foreach, I think) so if the content of an array element contains "1" then echo 1 else echo 2.

How can I read the content of each element to make this work?

$SeqID = array();
foreach($Seq as $key => $value) {
    if($value == 1){
    echo "1";
    } else {
    echo "2";
    }
}

The able is only my way of describing what I need.

Many thanks in advance for your time.

Aucun commentaire:

Enregistrer un commentaire