循环数组php(foreach)

时间:2016-04-16 15:50:46

标签: php

嗨,大家好,我有一个问题,我有一个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>

对不起我的英语我试图尽我所能让你容易动摇我想要的东西。

2 个答案:

答案 0 :(得分:1)

如果你没有像这样使用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>";
        }
    }
}

答案 1 :(得分:0)

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>";

}