循环数组php(foreach)


Loop array php (foreach)

大家好,我有个问题,我有一个250值的数组,我想像这个一样输出所有这些值

foreach($myArray as $x => $x_value) {
     $pic = strtolower($x);
     // here i want to echo only 83 value like this 
     // <td>83 value</td> <td>83 value</td> <td>83 value</td>
     echo "<img src='http://<my website>/$pic.png'/>$x_value";
     echo "<br>";
}

伙计们,这是我的阵列

    $countries = array
    (
        'AF' => 'Afghanistan',
        'AX' => 'Aland Islands',
        'AL' => 'Albania',
        // 250 country
    );
// 83 country in each colmun
// i want to echo <td> 83 country here </td> and <td> 83 country here </td> and <td> 83 country here </td>

我对我的英语感到抱歉,我正在尽力让你更容易理解我想要的东西。

如果不使用这样的for()循环,您可以创建自己的计数器;

// Array for counting total and 83
$count = array(
    "count" => 0, 
    "countNew" => 0
);
echo "<td>"; //Start with opening a <td>
foreach($myArray as $x => $x_value) {
    $count["count"]++; // Keep counting till end of array
    $count["countNew"]++; // Count for 250 / 3
    $pic = strtolower($x);
    // Get interval arraylengt / 3
    $tdThis = intval(floor(count($myArray) / 3));
    echo $x_value;
    if($count["countNew"] >= $tdThis){
        // Whenever we reach the 83 reset to 0 for new count to 83
        $count["countNew"] = 0;
        // Close </td> on every 83 countries
        echo "</td>";
        // If we don't reach the array end add new td 
        if($count["count"] === count($myArray)){
            echo "<td>";
        }
    }
}
foreach($myArray as $x => $x_value) {
 $pic = strtolower($x);
 // here i want to echo only 83 value like this 
 // <td>83 value</td> <td>83 value</td> <td>83 value</td>
 if($x==83)
    echo "<td>$x</td> <td>$x_value</td>";
 echo "<img src='http://<my website>/$pic.png'/>$x_value";
 echo "<br>";

}