PHP HTML和数组 - 以特定方式显示数组的结果

时间:2018-05-31 09:29:27

标签: php html arrays foreach

我有一个显示数组结果的问题。

我想以特定的方式在html表中显示数组中的信息。我做了一张照片,展示了我希望结果如何 我有这个数组:

$array[] = array('date' => $datesMonth, 'buro' => $buro_name, 'pechalba' => $pechalba, 'ime' => $ime);

echo '<pre>'.print_r($array,1).'</pre>';

Array
(
    [0] => Array
        (
            [date] => 2018-05-01
            [buro] => Kate
            [pechalba] => 48.66
            [ime] => silvi
        )

    [1] => Array
        (
            [date] => 2018-05-02
            [buro] => Kate
            [pechalba] => 135.09
            [ime] => silvi
        )

    [2] => Array
        (
            [date] => 2018-05-03
            [buro] => Kate
            [pechalba] => 288.03
            [ime] => cveti
        )

    [3] => Array
        (
            [date] => 2018-05-04
            [buro] => Kate
            [pechalba] => 95.36
            [ime] => silvi
        )

    [4] => Array
        (
            [date] => 2018-05-05
            [buro] => Kate
            [pechalba] => 74.62
            [ime] => maya
        )

    [5] => Array
        (
            [date] => 2018-05-01
            [buro] => Sisi
            [pechalba] => 54.3
            [ime] => ivo
        )

    [6] => Array
        (
            [date] => 2018-05-02
            [buro] => Sisi
            [pechalba] => 169.01
            [ime] => ivo
        )

    [7] => Array
        (
            [date] => 2018-05-03
            [buro] => Sisi
            [pechalba] => 216.31
            [ime] => ivo
        )

    [8] => Array
        (
            [date] => 2018-05-04
            [buro] => Sisi
            [pechalba] => 285.6
            [ime] => ivo
        )

    [9] => Array
        (
            [date] => 2018-05-05
            [buro] => Sisi
            [pechalba] => 11.7
            [ime] => ivo
        )

    [10] => Array
        (
            [date] => 2018-05-01
            [buro] => Gala
            [pechalba] => 20.44
            [ime] => maya
        )

    [11] => Array
        (
            [date] => 2018-05-02
            [buro] => Gala
            [pechalba] => 55.49
            [ime] => maya
        )

    [12] => Array
        (
            [date] => 2018-05-03
            [buro] => Gala
            [pechalba] => 161.23
            [ime] => maya
        )

    [13] => Array
        (
            [date] => 2018-05-04
            [buro] => Gala
            [pechalba] => 69.29
            [ime] => cveti
        )

    [14] => Array
        (
            [date] => 2018-05-05
            [buro] => Gala
            [pechalba] => 36.13
            [ime] => silvi
        )

)

我想以这种方式在html表中显示数组中的信息:

enter image description here

是否可以使用php执行此操作,如果是,我该怎么做?

1 个答案:

答案 0 :(得分:1)

这会从数组生成表格,样式表由您决定。

$groups = [];
foreach ($array as $data) {
    if (!in_array($data['buro'], $groups)) {
        $groups[$data['buro']][] = $data;
    }
}
// create header
echo "<tr>";
foreach($groups as $key => $group) {
    echo "<th colspan='3'>" . $key . "</th>";
}
echo "</tr>";

// create body
$stack = count(array_values($groups)[0]);

for ($i = 0; $i < $stack; $i++) {
    echo "<tr>";
    foreach ($groups as $group) {
        echo "<td>" . $group[$i]['date'] . "</td>";
        echo "<td>" . $group[$i]['pechalba'] . "</td>";
        echo "<td>" . $group[$i]['ime'] . "</td>";
    }
    echo "</tr>";
}

更新:我在本地运行并修复了我的错误,现在它似乎按预期工作了