PHP中的表显示了数组

时间:2018-06-08 11:05:33

标签: php html-table multiple-columns

我编写了用于从数组变量编写表的代码,但它显示了重复的列:

这是数组值:

    $data = [
    [
        "title" => "The World's End",
        "genre" => "Sci-fi",
        "year"  => 2013,
        "gross" => 21
    ],
    [
        "title" => "Scott Pilgrim vs. the World",
        "genre" => "Sadness",
        "year"  => 2010,
        "gross" => 21
    ],
    [
        "title" => "Hot Fuzz",
        "genre" => "Buddy Cop",
        "year"  => 2007,
        "gross" => 21
    ],
    [
        "title" => "Shaun of the Dead",
        "genre" => "Zombie",
        "year"  => 2007,
        "gross" => 21
    ],
];

表编写代码:

    <table>
    <tr>
        <?php
        foreach ($data as $item){
            foreach ($item as $key => $drop){
                echo '<th>' . $key . '</th>';
            }
        }
        ?>
    </tr>
    <?php foreach ($data as $item) : ?>
    <tr>
        <td><?= $item['title']; ?></td>
        <td><?= $item['genre']; ?></td>
        <td><?= $item['year']; ?></td>
        <td>€ <?= $item['gross']; ?></td>
        <?php endforeach; ?>
    </tr>
    <tr>
        <td></td>
        <td></td>
        <td></td>
        <td>€
            <?php
            $gross_sum = 0;
            foreach ($data as $gross_value)
            {
                $gross_sum += $gross_value['gross'];
            }
            echo $gross_sum;
            ?>
        </td>
    </tr>
</table>

如何从代码内容中删除表格中重复的右列?

4 个答案:

答案 0 :(得分:1)

这是因为你在开头有一个嵌套的foreach循环,因此你将遍历$data中的每个条目,然后再次迭代密钥。对于标题,您只需要迭代一次键。看看下面的代码,我对你的第一个foreach循环进行了一些小改动。

<?php

$data = [
    [
        "title" => "The World's End",
        "genre" => "Sci-fi",
        "year"  => 2013,
        "gross" => 21
    ],
    [
        "title" => "Scott Pilgrim vs. the World",
        "genre" => "Sadness",
        "year"  => 2010,
        "gross" => 21
    ],
    [
        "title" => "Hot Fuzz",
        "genre" => "Buddy Cop",
        "year"  => 2007,
        "gross" => 21
    ],
    [
        "title" => "Shaun of the Dead",
        "genre" => "Zombie",
        "year"  => 2007,
        "gross" => 21
    ],
];
?>

<html>
    <table>
    <tr>
        <?php
        foreach ($data[0] as $key => $item){
            // foreach ($item as $key => $drop){
                echo '<th>' . $key . '</th>';
            // }
        }
        ?>
    </tr>
    <?php foreach ($data as $item) : ?>
    <tr>
        <td><?= $item['title']; ?></td>
        <td><?= $item['genre']; ?></td>
        <td><?= $item['year']; ?></td>
        <td>€ <?= $item['gross']; ?></td>
        <?php endforeach; ?>
    </tr>
    <tr>
        <td></td>
        <td></td>
        <td></td>
        <td>€
            <?php
            $gross_sum = 0;
            foreach ($data as $gross_value)
            {
                $gross_sum += $gross_value['gross'];
            }
            echo $gross_sum;
            ?>
        </td>
    </tr>
</table>
</html

答案 1 :(得分:0)

尝试替换

<tr>
    <?php
    foreach ($data as $item) {
        foreach ($item as $key => $drop) {
            echo '<th>' . $key . '</th>';
        }
    }
    ?>
</tr>

用这个:

<tr>
    <?php
    foreach (array_keys($data[0]) as $item) {
        echo '<th>' . $item . '</th>';
    }
    ?>
</tr>

函数array_keys返回键以显示在标题中。

答案 2 :(得分:0)

您可以使用current获取数组的第一个元素,使用array_keys获取密钥。在foreach中结合使用可以解决您的问题。

foreach (array_keys(current($data)) as $key) {
    echo "<th>$key</th>";
}

除此之外,通常更好的方式是静态定义标题(除了,你不知道表的内容),

$header = ['title', 'genre', 'year', 'gross'];
foreach ($header as $text) {
   echo "<th>$text</th>";
}

答案 3 :(得分:-1)

我把它作为一个机会按照我的意愿写出来。希望它能为您提供一些用途。

<?php
$data = [
    [
        "title" => "The World's End",
        "genre" => "Sci-fi",
        "year"  => 2013,
        "gross" => 21,
    ],
    [
        "title" => "Scott Pilgrim vs. the World",
        "genre" => "Sadness",
        "year"  => 2010,
        "gross" => 21,
    ],
    [
        "title" => "Hot Fuzz",
        "genre" => "Buddy Cop",
        "year"  => 2007,
        "gross" => 21,
    ],
    [
        "title" => "Shaun of the Dead",
        "genre" => "Zombie",
        "year"  => 2007,
        "gross" => 21,
    ],
];

// Let's grab your headers
$headers = array_keys($data[0]);

// Format them by capitalizing the first letter
$headers = array_map('ucfirst', $headers);

/**
 * You could also inline it into one assignment
 * This decreases readability, but removes the quick reassignment of $header
 *
 * $headers = array_map('ucfirst', array_keys($data[0]));
 */

/**
 * Let's calculate the gross here instead of in the presentation (HTML)
 */
$total_gross = array_reduce($data, function ($gross, $movie) {
    $gross += $movie['gross'];

    return $gross;
}, 0);
?>

<table>
    <tr>
        <?php foreach ($headers as $header) :
            printf('<th>%s</th>', $header);
        endforeach; ?>
    </tr>
    <?php foreach ($data as $movie) : ?>
        <tr>
            <?php
            printf('<td>%s</td>', $movie['title']);
            printf('<td>%s</td>', $movie['genre']);
            printf('<td>%s</td>', $movie['year']);
            printf('<td>€ %s</td>', $movie['gross']);
            ?>
        </tr>
    <?php endforeach; ?>
    <tr>
        <td colspan="3"></td>
        <?php printf('<td>€ %s</td>', $total_gross); ?>
    </tr>
</table>