获取PHP数组值并将其打印在不同的列上

前端之家收集整理的这篇文章主要介绍了获取PHP数组值并将其打印在不同的列上前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这里我有一个包含5个名字的循环< table>行.此表应该是一个排名,其中第一列将显示一行参与者名称,其他10列应显示分数.

对于我想要添加分数的空列,我想为每个参与者创建一个数组,并使其循环通过< td>.在阵列中有10个数字,其中每个数字应显示在每个空的< td>上.剩下.

//Names
$names = array("Mike","Kyle","Johnny","Will","Vasques");

//scores values for each name
$scores = array(
    "Mike"    => array(04,03,00,07,04,01,04),"Kyle"    => array(07,"Johnny"  => array(07,03),"Will"    => array(03,"Vasques" => array(03,07)
);

//start loop for rows
for($x=0; $x<count($names); $x++) {
  echo "<tr>";
  //column for the names
  echo "<td>$names[$x]</td>";
  //loop for columns where the score should be displayed
  for($td=2; $td<=10; $td++) {
    echo "<td></td>";
  }
  echo "</tr>";
}

它应该显示如下:

Mike   04  03  00  07  04  07  00  01  00  07  04
Kyle   07  01  00  03  04  01  00  07  03  04  04
Johnny 03  01  00  03  04  07  00  01  00  07  07

等等…

尝试这个
echo "<table>";
foreach($scores as $key=>$value)
{
     echo "<tr>";
     echo "<td>$key</td>";
     for($x=0; $x<count($value); $x++) 
     {
         echo "<td>".sprintf('%02d',$value[$x])."</td>";
     }
     echo "</tr>";
}
echo "</table>";

示例完整代码视图

<?PHP
//Names
$names = array("Mike",'-',07)
);
echo "<table>";
foreach($scores as $key=>$value)
{
     echo "<tr>";
     echo "<td>$key</td>";
     for($x=0; $x<count($value); $x++) 
     {
          if (is_numeric($value[$x])) 
     {
        echo "<td>".sprintf('%02d',$value[$x])."</td>";
     }
     else
     {
         echo "<td>". $value[$x]."</td>";
     }
     }
     echo "</tr>";
}
echo "</table>";

产量

Mike    04  03  00  -   -   -   -   -   -   07  04
Kyle    07  01  00  03  04  01  00  07  03  04  04
Johnny  07  07  00  03  00  04  00  01  01  04  03
Will    03  04  00  03  04  07  00  01  00  07  04
Vasques 03  01  00  03  04  07  00  01  00  07  07
原文链接:https://www.f2er.com/php/138367.html

猜你在找的PHP相关文章