从数组创建 'pivot' table

Create a 'pivot' table from array

我有一个如下所示的数组:

Array (
    Array ( [date] => 04/2021 [name] => Fred [value] => 12.00 )
    Array ( [date] => 04/2021 [name] => Tom [value] => 160.00 )
    Array ( [date] => 04/2021 [name] => Mike [value] => 9.00 )
    Array ( [date] => 07/2021 [name] => Tony [value] => 200.00 )
    Array ( [date] => 07/2021 [name] => Fred [value] => 43.00 )
    Array ( [date] => 07/2021 [name] => Tom [value] => 114.00 )
    Array ( [date] => 07/2021 [name] => Mike [value] => 28.00 )
)

我正在尝试将数据放入一个简单的 HTML table 中,其中名称在 x 轴上,日期在 y 轴上以及它们相交的值。

例如:

          Fred     Tom     Mike    Tony
04/2021   12.00    160.00  9.00    0
07/2021   43.00    114.00  28.00   200.00

我不知道我是不是想多了,但我无法让它工作——我可以通过提取名称的唯一值来让 x 轴工作,但是当涉及到 y 轴和我无法获取数据来显示我的需要。

我要么以重复的列结束,要么以单个列结束,等等。这是我已经完成但仍在摸索如何做到这一点的地方:

<?php
$unique_names = array_unique(array_map(function($elem){
    return $elem['name'];
}, $data));
?>

<thead>
    <tr>
        <th>&nbsp;</th>
        <?php foreach ($unique_names as $i) { ?>
            <th><?php print $i;?></th>
        <?php } ?>
    </tr>
</thead>

<tbody>
<?php
foreach ($data as $row) {
    for ($i=0; $i<=count($unique_names);$i++) {
        if ($i==0) {
            print "<tr><td>".$row['date']."<td>";
        } else {
            print "<td>".$row['value']."<td>";
        }

        if ($i==count($unique_dates)) {
            print "</tr>";
        }
    }
}
?>
</tbody>

首先,将您的数据重新组织到一个数组中,该数组在第一层使用日期作为键,在第二层使用名称。同时收集姓名:

$input = [
    ['date' => '04/2021', 'name' => 'Fred', 'value' => '12.00'],
    ['date' => '04/2021', 'name' => 'Tom', 'value' => '160.00'],
    ['date' => '04/2021', 'name' => 'Mike', 'value' => '9.00'],
    ['date' => '07/2021', 'name' => 'Tony', 'value' => '200.00'],
    ['date' => '07/2021', 'name' => 'Fred', 'value' => '43.00'],
    ['date' => '07/2021', 'name' => 'Tom', 'value' => '114.00'],
    ['date' => '07/2021', 'name' => 'Mike', 'value' => '28.00'],
];

$data = $names = [];

foreach($input as $item) {
    $data[$item['date']][$item['name']] = $item['value'];
    $names[] = $item['name'];
}
$names = array_unique($names);

然后创建您的 table。遍历名称以首先创建 header 行。 然后遍历重组后的数据数组(为每个日期创建一行),并在该循环中再次遍历您的名字 - 如果您在数据数组中找到与该名称匹配的条目,则输出该值,否则什么也不输出。

<table>
    <tr>
        <th></th>
        <?php foreach($names as $name): ?>
        <th><?php echo $name; ?></th>
        <?php endforeach; ?>
    </tr>
    <?php foreach($data as $date => $userData): ?>
    <tr>
        <td><?php echo $date; ?></td>
        <?php foreach($names as $name): ?>
        <td><?php echo $userData[$name]??'-'; ?></td>
        <?php endforeach; ?>
    </tr>
    <?php endforeach; ?>
</table>

结果:

<table>
    <tr>
        <th></th>
                <th>Fred</th>
                <th>Tom</th>
                <th>Mike</th>
                <th>Tony</th>
            </tr>
        <tr>
        <td>04/2021</td>
                <td>12.00</td>
                <td>160.00</td>
                <td>9.00</td>
                <td>-</td>
            </tr>
        <tr>
        <td>07/2021</td>
                <td>43.00</td>
                <td>114.00</td>
                <td>28.00</td>
                <td>200.00</td>
            </tr>
    </table>

Here is your answer, great exercise.

    <html>
    <head>
    <style>
table, th, td {
  border: 1px solid black;
}
</style>
    </head>
    <body>
<?php
$data = Array (
    Array ( 'date' => '04/2021' ,'name' => 'Fred' ,'value' => '12.00' ),
    Array ( 'date' => '04/2021' ,'name' => 'Tom' ,'value' => '160.00' ),
    Array ( 'date' => '04/2021' ,'name' => 'Mike', 'value' => '9.00' ),
    Array ( 'date' => '07/2021' ,'name' => 'Tony' ,'value' => '200.00' ),
    Array ( 'date' => '07/2021' ,'name' => 'Fred' ,'value' => '43.00' ),
    Array ( 'date' => '07/2021' ,'name' => 'Tom' ,'value' => '114.00' ),
    Array ( 'date' => '07/2021' ,'name' => 'Mike' ,'value' => '28.00' )
);
foreach ($data as $key => $value) {
    $tmp[$value['name']][$value['date']] = $value;
}

$unique_names = array_unique(array_map(function($elem){
    return $elem['name'];
}, $data));
$unique_dates = array_unique(array_map(function($elem){
    return $elem['date'];
}, $data));

?>
<table>
<thead>
    <tr>
        <th>Dates</th>
        <?php foreach ($unique_names as $i) { ?>
            <th><?php print $i;?></th>
        <?php } ?>
    </tr>
</thead>
<?php
foreach ($unique_dates as $date)
{   
    echo "<tr>";
    echo "<td>".$date."</td>";
    foreach ($unique_names as $name) {
        if(array_key_exists($date,$tmp[$name]))
        echo "<td>".$tmp[$name][$date]['value']."</td>";
        else
        echo "<td>0</td>";
    }
    echo "</tr>";
}
?>
<tbody>

</tbody>
</table>
</body>
</html>