让PHP数组值,并在打印循环 [英] Get PHP array values and print in a loop

查看:133
本文介绍了让PHP数组值,并在打印循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有9行的表。第一列应打印排名的每个参与者的名称。首先,我没有包含它们的名称的数组:

  $ =名称阵列(迈克,凯尔,约翰尼,将要,Vasques);

有关这个任务我codeD循环使用5行10列。所以,正如我所说,第一栏应打印每个人的名字。

 为($ X = 1; $ X< =计数($名); $ X ++){
  回声< TR>中;
  为($ TD = 1; $ TD< = 10; $ TD ++){
    回声< TD>< / TD>中;
  }
  回声< / TR>中;
}

正如你可以看到,有一个℃的循环; TR> ℃的循环内; TD> !我如何在第一行中打印每名?


解决方案

  $名=阵列(迈克,凯尔,约翰尼,将要,Vasques );
为($ TD = 0; $ TD< = 9; $ TD ++){
  回声< TR>中;
  如果($ TD == 0){
    的foreach($名称为$名){
      回声< TD> $名和LT; / TD>中;
    }
  }
  回声< TD>< / TD>中;
  回声< / TR>中;
}

I have a table with 9 rows. The first column should print the name of each participant of the ranking. First of, I did an array containing the names of them:

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

For this task I coded a loop with 5 rows and 10 columns. So, as I mentioned, the first column should print the name of each one.

for($x=1; $x<=count($names); $x++) {
  echo "<tr>";
  for($td=1; $td<=10; $td++) {
    echo "<td></td>";
  }
  echo "</tr>";
}

As you can see, there is a loop for <tr> and a loop inside for <td>! How do I print each name in the first row?

解决方案

$names = array("Mike", "Kyle", "Johnny", "Will", "Vasques");
for($td=0; $td<=9; $td++) {
  echo "<tr>";
  if ($td == 0) {
    foreach ($names as $name) {
      echo "<td>$name</td>";
    }
  }
  echo "<td></td>";
  echo "</tr>";
}

这篇关于让PHP数组值,并在打印循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆