如何在html表格中获取数组输出 [英] How to get array output in html table

查看:913
本文介绍了如何在html表格中获取数组输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是以下几点:



我有一个SQL查询给我一个输出。

  $ cities [$ row ['stad']] [$ row ['状态']] ++ 

这给了我一个像这样的输出(在
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'阿姆斯特丹] =>阵列

[41] => 2
[21] => 91
[43] => 16
[42 ] => 2
[20] => 30
[4] => 4
[70] => 3
[84] => 8
[46] => 4
[45] => 5
[999] => 26
[47] => 2
[3] => 8
[44] => 1
[40] => 1
[93] => 5
[56] => 3
[61] => 3
[79] => 3
[48] => 2
[50] => 5
[10] = > 10
[52 ] => 2
[120] => 1
[95] => 1
[1] => 65
[90] => 6

我想放入一个html表格,如下所示:

 城市41 21 43 42 20 7 ......等
阿姆斯特丹2 91 16 2 30 4 ......等

重要的是要知道有超过1个城市。



这是我现在所拥有的:

  echo'< table cellpadding =10cellspacing =10border = 1 >'; 

foreach($ cities as $ city){
echo'< tr>';
回声'< td>'。 $ row ['stad']。 < / TD>;
回声'< td>'。 $ city [$ row ['status']]。 < / TD>;

echo'< / tr>';
}

echo'< / table>';


解决方案

您需要另一个列出所有标题值的数组;在我的代码中,我将这个数组称为 $ headings 。这是因为每个城市的值可能按照不同的顺序排列,并且可能会丢失关键字,因此只需在城市数组中循环就不会在每一行获得一致的值。

  $ headings =数组(41,21,43,42,20,7等); 

echo'< table cellpadding =10cellspacing =10border =1>';
echo'< tr> th City< / th>';
foreach($标题为$ h){
echo< th> $ h< / th>;
}
echo'< / tr>';

foreach($ cities为$ cityname => $ city){
echo'< tr>';
回显< td> $ cityname< / td>;
foreach($标题为$ h){
echo'< td>'。 (isset($ city [$ h])?$ city [$ h]:'')。 < / TD>;
}
echo'< / tr>';
}

echo'< / table>';


What I would like to do is the following:

I have an SQL query that give's me an output. On that output 1 make an selection in php.

$cities[$row['stad']][$row['status']]++

This gives me an output like this (within a <pre> tag ):

Array
(
    [Amsterdam] => Array
        (
            [41] => 2
            [21] => 91
            [43] => 16
            [42] => 2
            [20] => 30
            [4] => 4
            [70] => 3
            [84] => 8
            [46] => 4
            [45] => 5
            [999] => 26
            [47] => 2
            [3] => 8
            [44] => 1
            [40] => 1
            [93] => 5
            [56] => 3
            [61] => 3
            [79] => 3
            [48] => 2
            [50] => 5
            [10] => 10
            [52] => 2
            [120] => 1
            [95] => 1
            [1] => 65
            [90] => 6
        )

I would like to put in an html table like so :

City        41  21  43  42  20  7   …… etc 
amsterdam   2   91  16  2   30  4   …… etc 

important to know is there are more than 1 city.

This is what I have at the moment :

echo '<table cellpadding="10" cellspacing="10" border="1">';

foreach($cities as $city) { 
    echo '<tr>';
    echo '<td>' . $row['stad'] . '</td>';
    echo '<td>' . $city[$row['status']] . '</td>';

    echo '</tr>';
}

echo '</table>';

解决方案

You need another array that lists all the heading values; in my code below I call this array $headings. This is because the values for each city may have their keys in a different order, and there may be missing keys, so just looping through the city arrays won't get consistent values on each row.

$headings = array(41, 21,  43,  42,  20,  7, etc.); 

echo '<table cellpadding="10" cellspacing="10" border="1">';
echo '<tr><th>City</th>';
foreach ($headings as $h) {
    echo "<th>$h</th>";
}
echo '</tr>';

foreach($cities as $cityname => $city) { 
    echo '<tr>';
    echo "<td>$cityname</td>";
    foreach ($headings as $h) {
        echo '<td>' . (isset($city[$h]) ? $city[$h] : '') . '</td>';
    }    
    echo '</tr>';
}

echo '</table>';

这篇关于如何在html表格中获取数组输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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