如何在php中显示数组内容
我有一个数组,看起来像:如何在php中显示数组内容,php,arrays,html-table,show,Php,Arrays,Html Table,Show,我有一个数组,看起来像: Array ( [0] => Array ( [total words] => 1476 ) [1] => Array ( [keyword] => difference [count] => 82 [percent] => 5.56 ) [2] =
Array
(
[0] => Array
(
[total words] => 1476
)
[1] => Array
(
[keyword] => difference
[count] => 82
[percent] => 5.56
)
[2] => Array
(
[keyword] => 2010
[count] => 37
[percent] => 2.51
)
[3] => Array
(
[keyword] => very
[count] => 22
[percent] => 1.49
)
)
我想在一个三列三行的表中显示数组内容。每行包含关键字、计数和百分比,作为列和总数。单词将显示在表格标题中
请帮帮我!我试图运行for循环,但不知道如何显示数组内容,因为它看起来像一个多维数组。请帮帮我。这应该是你想要的
print '<table>';
$headers = array_keys(reset($array));
print '<tr>';
foreach($headers as $header){
print '<th>'.$header.'</th>';
}
print '<tr>';
foreach($array as $row){
print '<tr>';
foreach($row as $col){
print '<td>'.$col.'</td>';
}
print '</tr>';
}
print '</table>';
打印“”;
$headers=数组_键(重置($array));
打印“”;
foreach($headers作为$header){
打印“.$header.”;
}
打印“”;
foreach($array作为$row){
打印“”;
foreach(行作为$col){
打印“.$col.”;
}
打印“”;
}
打印“”;
这应该是你想要的
print '<table>';
$headers = array_keys(reset($array));
print '<tr>';
foreach($headers as $header){
print '<th>'.$header.'</th>';
}
print '<tr>';
foreach($array as $row){
print '<tr>';
foreach($row as $col){
print '<td>'.$col.'</td>';
}
print '</tr>';
}
print '</table>';
打印“”;
$headers=数组_键(重置($array));
打印“”;
foreach($headers作为$header){
打印“.$header.”;
}
打印“”;
foreach($array作为$row){
打印“”;
foreach(行作为$col){
打印“.$col.”;
}
打印“”;
}
打印“”;
在这种情况下,内爆是你的朋友:
$arrayLength = count($myArray);
echo '<table>';
for($i=0;$i<$arrayLength;$i++){
echo '<tr><td>'.
.implode('</td><td>',$myArray[$i])
.'</td></tr>';
}
echo '</table>';
$arrayLength=计数($myArray);
回声';
在这种情况下,$i=0;$i内爆是你的朋友:
$arrayLength = count($myArray);
echo '<table>';
for($i=0;$i<$arrayLength;$i++){
echo '<tr><td>'.
.implode('</td><td>',$myArray[$i])
.'</td></tr>';
}
echo '</table>';
$arrayLength=计数($myArray);
回声';
对于($i=0;$i,假设数组存储在变量$a中
foreach($item in $a) {
echo $item['keyword'] . " " . $item['count'] . " " . $item['percent'] . "<br>\n";
}
foreach($a中的项目){
回显$item['keyword'].'.$item['count'.'.'.$item['percent'.][br>\n”;
}
假设数组存储在变量$a中
foreach($item in $a) {
echo $item['keyword'] . " " . $item['count'] . " " . $item['percent'] . "<br>\n";
}
foreach($a中的项目){
回显$item['keyword'].'.$item['count'.'.'.$item['percent'.][br>\n”;
}
您可以使用foreach($array=>$value)
循环来迭代数组。
这段代码应该做到以下几点:
<table>
<tr>
<th>Keyword</th>
<th>Count</th>
<th>%</th>
</tr>
<?php foreach ( $data as $row ): ?>
<tr>
<td><?php echo $row['keyword']; ?></td>
<td><?php echo $row['count']; ?></td>
<td><?php echo $row['percent']; ?></td>
</tr>
<?php endforeach; ?>
</table>
关键词
计数
%
您可以使用foreach($array=>$value)
循环来迭代数组。
这段代码应该做到以下几点:
<table>
<tr>
<th>Keyword</th>
<th>Count</th>
<th>%</th>
</tr>
<?php foreach ( $data as $row ): ?>
<tr>
<td><?php echo $row['keyword']; ?></td>
<td><?php echo $row['count']; ?></td>
<td><?php echo $row['percent']; ?></td>
</tr>
<?php endforeach; ?>
</table>
关键词
计数
%
数组\u值-返回数组的所有值
print_r(array_values($array));
array_values-返回数组的所有值
print_r(array_values($array));
除了上面Godea Andrei的回答,如果您使用的是print\r,只需使用
print_r($array)
在print\r调用中调用array\u值将去除数组元素的所有关联命名。如果仅使用数字数组索引,它仍将显示索引值。例如
$a = array("red", "green", "blue");
打印($a)将显示
Array ( [0] => red [1] => green [2] => blue )
Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
鉴于
$b = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
打印($b)将显示
Array ( [0] => red [1] => green [2] => blue )
Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
如果使用数组_值,则第二个示例的输出为
Array ( [0] => 35 [1] => 37 [2] => 43 )
除了上面Godea Andrei的回答,如果您使用的是print\r,只需使用
print_r($array)
在print\r调用中调用array\u值将去除数组元素的所有关联命名。如果仅使用数字数组索引,它仍将显示索引值。例如
$a = array("red", "green", "blue");
打印($a)将显示
Array ( [0] => red [1] => green [2] => blue )
Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
鉴于
$b = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
打印($b)将显示
Array ( [0] => red [1] => green [2] => blue )
Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
如果使用数组_值,则第二个示例的输出为
Array ( [0] => 35 [1] => 37 [2] => 43 )
为什么它需要是for
循环?foreach
有什么问题?为什么它需要是for
循环?foreach
有什么问题?