简体   繁体   中英

PHP Loop through an array second time (foreach)

I have a foreach to loop through the data provided by a PDO SQL query:

foreach ($team as $row){
    $count++;
    $teamNumber = 'team'.$count;
    if ($currentScores[0]['team'.$count] == ""){
        $red = "red";
    }
    echo "<strong><font color='".$red."'>".$row['name']."</font></strong>";
    echo $currentScores[0]['team'.$count];
    if ($count < 2) 
        echo " vs ";
}

Now, I want to loop again through the $team array but it just returns the last value of the array during the second loop.

I tried the same thing:

foreach ($team as $row) {
.....
.......
}

How could I run again through the array?

Simple enough, just do a foreach loop on $row as you have previously.

foreach($array as $key => $val) {
    foreach($val as $a => $b) {
        echo $b;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM