简体   繁体   English

在1个html表单元中打印整个PHP数组

[英]Print entire PHP array in 1 html table cell

so I'm trying to get my PHP array to print an entire array within one td, so I want the table to look like this: 所以我试图让我的PHP数组在一个td内打印整个数组,所以我希望表看起来像这样:

First  ---  Last  ---  Values  
John   --  Smith  --  1,2,4,6 

$comp_labs holds the values (1,2,4,6 for example) $comp_labs保存值(例如1,2,4,6)

Here's the code I've got at the moment: 这是我目前获得的代码:

echo "<tr>";
echo "
<td> ".$firstName."</td>
<td> ".$lastName."</td>
<td> ".$comp_labs."</td>
</tr>\n";
echo "</table>";

it's printing the name fine but only the last element of the array. 它打印的名称很好,但仅打印数组的最后一个元素。 I tried looping through just that td printing array[i] like so: 我试图像这样循环遍历该td打印数组[i]:

echo "<tr>";
echo "
<td> ".$firstName."</td>
<td> ".$lastName."</td>
<td> "for($i=0; $i < count($comp_labs); $i++){.$comp_labs[i].}"</td>
</tr>\n";
echo "</table>";

but it didn't like that. 但这不是那样。 However if I just Echo out the $comp_labs array outside of the table it prints the entire array fine, which I find a little confusing. 但是,如果我只是在表外回显$comp_labs数组,它将很好地打印整个数组,这让我感到有些困惑。 I've had a look online but every forum I've come across has been people trying to loop through and print 1 value per t d, not all values in one td . 我在网上看过,但是我遇到的每个论坛都是人们试图遍历并每t d打印1个值,而不是每td都打印所有值。

If anyone is able to help out here I would greatly appreciate it, thanks in advance! 如果有人能在这里提供帮助,我将不胜感激,在此先感谢您!

You need implode() 您需要implode()

 <td> ".implode(",", $comp_labs )."</td>

So it becomes : 变成:

echo "<tr>
              <td> ".$firstName."</td>
              <td> ".$lastName."</td>
              <td> ".implode(",", $comp_labs)."</td>
     </tr>\n";

放大数组,将其转换为字符串。

$comp  = implode(", ",$comp_labs);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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