显示带有两个标题的 PHP 数组中的 HTML 表

我有一个包含运动结果的数组,如下所示:


Array

(

    [0] => Array

        (

            [name] => Alexander

            [distance] => 60

            [value] => 9

        )


    [1] => Array

        (

            [name] => Alexander

            [distance] => 100

            [value] => 10

        )


    [2] => Array

        (

            [name] => Alexander

            [distance] => 200

            [value] => 11

        )


    [3] => Array

        (

            [name] => Alexander

            [distance] => 400

            [value] => 12

        )


    [4] => Array

        (

            [name] => Dmitriy

            [distance] => 60

            [value] => 5

        )


    [5] => Array

        (

            [name] => Dmitriy

            [distance] => 100

            [value] => 6

        )


    [6] => Array

        (

            [name] => Dmitriy

            [distance] => 200

            [value] => 7

        )


    [7] => Array

        (

            [name] => Dmitriy

            [distance] => 400

            [value] => 8

        )


    [8] => Array

        (

            [name] => Sergei

            [distance] => 60

            [value] => 1

        )


    [9] => Array

        (

            [name] => Sergei

            [distance] => 100

            [value] => 2

        )


    [10] => Array

        (

            [name] => Sergei

            [distance] => 200

            [value] => 3

        )


    [11] => Array

        (

            [name] => Sergei

            [distance] => 400

            [value] => 4

        )

)

我需要在 html 表中以特定方式显示数组数据,应该是这样的。顶部标题应包含“距离”键的所有唯一值,左侧标题应包含“名称”键的所有唯一值:


           |  60  |  100  |  200  |  400

===========================================

Alexnader  |  9   |  10   |   11  |  12

===========================================

Dmitriy    |  5   |   6   |   7   |   8

===========================================

Sergei     |  1   |   2   |   3   |   4

我怎样才能用最少的循环或使用 php 数组方法以最快的方式做到这一点?谢谢你。


MYYA
浏览 96回答 2
2回答

暮色呼如

获取表格的唯一距离和名称。$names = array_unique(array_map(fn($a) => $a['name'], $data));$dists = array_unique(array_map(fn($a) => $a['distance'], $data));制作一个按名称和距离查找表值的函数。function findValue($dist, $name, $data): string {&nbsp; &nbsp; foreach($data as $d) {&nbsp; &nbsp; &nbsp; &nbsp; if($d['name'] != $name || $d['distance'] != $dist ) continue;&nbsp; &nbsp; &nbsp; &nbsp; return (string) $d['value'];&nbsp; &nbsp; }&nbsp; &nbsp; return '';}现在输出以头部和距离开头的表格echo "<table>\n";echo "<tr><th></th>", implode('', array_map(fn($col) => "<th>$col</th>", $dists)), "</tr>\n";使用我们的 find 函数逐行列出每个名称的数据foreach($names as $name) {&nbsp; &nbsp; $row = [];&nbsp; &nbsp; foreach($dists as $dist) {&nbsp; &nbsp; &nbsp; &nbsp; $row[] = '<td>' . findValue($dist, $name, $data) . '</td>';&nbsp; &nbsp; }&nbsp; &nbsp; $rowText = join('', $row);&nbsp; &nbsp; echo "<tr><td>$name</td>$rowText</tr>\n";}最后关上桌子。echo "</table>\n";结果是<table><tr><th></th><th>60</th><th>100</th><th>200</th><th>400</th></tr><tr><td>Alexander</td><td>9</td><td>10</td><td>11</td><td>12</td></tr><tr><td>Dmitriy</td><td>5</td><td>6</td><td>7</td><td>8</td></tr><tr><td>Sergei</td><td>1</td><td>2</td><td>3</td><td>4</td></tr></table>

饮歌长啸

我们必须先重组数组$array = Array(&nbsp; &nbsp; "0" => Array&nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; "name" => "Alexander",&nbsp; &nbsp; &nbsp; &nbsp; "distance" => 60,&nbsp; &nbsp; &nbsp; &nbsp; "value" => 9&nbsp; &nbsp; ),&nbsp; &nbsp; "1" => Array&nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; "name" => "Alexander",&nbsp; &nbsp; &nbsp; &nbsp; "distance" => 100,&nbsp; &nbsp; &nbsp; &nbsp; "value" => 10&nbsp; &nbsp; ),&nbsp; &nbsp; "2" => Array&nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; "name" => "Alexander",&nbsp; &nbsp; &nbsp; &nbsp; "distance" => 200,&nbsp; &nbsp; &nbsp; &nbsp; "value" => 11&nbsp; &nbsp; ),&nbsp; &nbsp; "3" => Array&nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; "name" => "Alexander",&nbsp; &nbsp; &nbsp; &nbsp; "distance" => 400,&nbsp; &nbsp; &nbsp; &nbsp; "value" => 12&nbsp; &nbsp; ),&nbsp; &nbsp; "4" => Array&nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; "name" => "Dmitriy",&nbsp; &nbsp; &nbsp; &nbsp; "distance" => 60,&nbsp; &nbsp; &nbsp; &nbsp; "value" => 5&nbsp; &nbsp; ),&nbsp; &nbsp; "5" => Array&nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; "name" => "Dmitriy",&nbsp; &nbsp; &nbsp; &nbsp; "distance" => 100,&nbsp; &nbsp; &nbsp; &nbsp; "value" => 6&nbsp; &nbsp; ),&nbsp; &nbsp; "6" => Array&nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; "name" => "Dmitriy",&nbsp; &nbsp; &nbsp; &nbsp; "distance" => 200,&nbsp; &nbsp; &nbsp; &nbsp; "value" => 7&nbsp; &nbsp; ),&nbsp; &nbsp; "7" => Array&nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; "name" => "Dmitriy",&nbsp; &nbsp; &nbsp; &nbsp; "distance" => 400,&nbsp; &nbsp; &nbsp; &nbsp; "value" => 8&nbsp; &nbsp; ),&nbsp; &nbsp; "8" => Array&nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; "name" => "Sergei",&nbsp; &nbsp; &nbsp; &nbsp; "distance" => 60,&nbsp; &nbsp; &nbsp; &nbsp; "value" => 1&nbsp; &nbsp; ),&nbsp; &nbsp; "9" => Array&nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; "name" => "Sergei",&nbsp; &nbsp; &nbsp; &nbsp; "distance" => 100,&nbsp; &nbsp; &nbsp; &nbsp; "value" => 2&nbsp; &nbsp; ),&nbsp; &nbsp; "10" => Array&nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; "name" => "Sergei",&nbsp; &nbsp; &nbsp; &nbsp; "distance" => 200,&nbsp; &nbsp; &nbsp; &nbsp; "value" => 3&nbsp; &nbsp; ),&nbsp; &nbsp; "11" => Array&nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; "name" => "Sergei",&nbsp; &nbsp; &nbsp; &nbsp; "distance" => 400,&nbsp; &nbsp; &nbsp; &nbsp; "value" => 4&nbsp; &nbsp; ));$table_heading = array_unique(array_column($array, "distance"));$restructured_array = [];foreach($array as $row){&nbsp; &nbsp; if(!isset($restructured_array[$row["name"]])){&nbsp; &nbsp; &nbsp; &nbsp; $restructured_array[$row["name"]] = [];&nbsp; &nbsp; }&nbsp; &nbsp; $restructured_array[$row["name"]][$row["distance"]] = $row["value"];}现在可以循环数组以打印表格。echo "<table><thead><tr>";echo "<th></th>";foreach($table_heading as $heading){&nbsp;&nbsp; &nbsp; echo "<th>".$heading."</th>";}echo "</tr></thead>";echo "<tbody>";&nbsp;foreach ($restructured_array as $key=>$rows){&nbsp; &nbsp; echo "<tr><th>".$key."</th>";&nbsp; &nbsp; foreach ($rows as $k=>$row){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo "<td>".$row."</td>";&nbsp; &nbsp; }}echo "</tbody></table>";输出将是&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;60&nbsp; &nbsp;100 200 400Alexander&nbsp; &nbsp;9&nbsp; &nbsp;10&nbsp; 11&nbsp; 12Dmitriy&nbsp; &nbsp; &nbsp;5&nbsp; &nbsp;6&nbsp; &nbsp;7&nbsp; &nbsp;8Sergei&nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp;2&nbsp; &nbsp;3&nbsp; &nbsp;4
打开App,查看更多内容
随时随地看视频慕课网APP