【问题标题】:Group multidimensional php array outputs on single row [closed]在单行上分组多维 php 数组输出[关闭]
【发布时间】:2020-01-01 03:56:02
【问题描述】:

如何使用表 #1 中的 foreach 实现表 #2 中的结果 查看示例代码和所需的输出。

<?php

$array = [
["id"=>1,"day"=>"1","amount"=>345],
["id"=>2,"day"=>"1","amount"=>66],
["id"=>1,"day"=>"3","amount"=>12],
["id"=>2,"day"=>"1","amount"=>69],
["id"=>1,"day"=>"4","amount"=>82],
["id"=>1,"day"=>"5","amount"=>12.5],
["id"=>2,"day"=>"3","amount"=>35],
["id"=>2,"day"=>"4","amount"=>89],
["id"=>2,"day"=>"5","amount"=>9],
["id"=>1,"day"=>"2","amount"=>15],
["id"=>2,"day"=>"4","amount"=>67]
];

$size = count($array);

$table = "<table><tr><th>Table #1</th><th>Monday</th><th>Tuesday</th><th>Wednesday</th><th>Thursday</th><th>Friday</th></tr>";

for($i = 0; $i < $size; $i++) {
$table .= ($array[$i]["id"] === 1)? "<tr class='service1'><td>Service #1</td>" : "<tr class='service2'><td>Service #2</td>";
$table .= ($array[$i]["day"] == 1)? "<td>{$array[$i]["amount"]}</td>" : "<td></td>";
$table .= ($array[$i]["day"] == 2)? "<td>{$array[$i]["amount"]}</td>" : "<td></td>";
$table .= ($array[$i]["day"] == 3)? "<td>{$array[$i]["amount"]}</td>" : "<td></td>";
$table .= ($array[$i]["day"] == 4)? "<td>{$array[$i]["amount"]}</td>" : "<td></td>";
$table .= ($array[$i]["day"] == 5)? "<td>{$array[$i]["amount"]}</td>" : "<td></td>";
$table .= "</tr>";
}
$table .= "</table>";

echo $table;

?>

想要的结果:https://jsfiddle.net/dvporua6/

【问题讨论】:

  • 在里面添加另一个循环,循环数天。
  • 这是什么逻辑?

标签: php html arrays multidimensional-array columnsorting


【解决方案1】:

我想到的是,您可能需要先重组来自初始数组的输出。

由于第二张桌子有空天,我建议先预先填充另一个数组,以便您一开始就处理它。

然后将多维数组细分为services -&gt; weeks -&gt; days 行格式。

为了更好地理解我的意思,这里是它的转储表示:

Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [1] => 345
                    [2] => 15
                    [3] => 12
                    [4] => 82
                    [5] => 12.5
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [1] => 66
                    [2] => 
                    [3] => 35
                    [4] => 89
                    [5] => 9
                )

            [1] => Array
                (
                    [1] => 69
                    [2] => 
                    [3] => 
                    [4] => 67
                    [5] => 
                )

        )

)

所以父数组由services 组成。在一项服务下,它需要有另一个级别,因为您可能会遇到几天的冲突,我假设这可以表示为几周。

当然,星期以下是天数和相应的数量。

现在,如果它们以正确的行格式放置,从该格式开始,创建一个类似于此的表应该相当简单:

现在要达到那个结构,我们需要先建立一个新结构。然后渲染一个空值的数组结构。

$array2 = []; // initial array container
$num_days_per_week = 5;
$num_group_type = array_count_values(array_column($array, 'id')); // get the services and how many days in each service
foreach ($num_group_type as $service => $batch) { // loop service group
    $batches = ceil($batch / $num_days_per_week); // get number of weeks for each service
    for ($i = 1; $i <= $batches; $i++) { // pre fill
        $array2[$service][] = array_combine(range(1, $num_days_per_week), array_fill(0, $num_days_per_week, null));
    }
}

这个块应该给我们一个像这样的空数组:

Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [1] => 
                    [2] => 
                    [3] => 
                    [4] => 
                    [5] => 
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [1] => 
                    [2] => 
                    [3] => 
                    [4] => 
                    [5] => 
                )

            [1] => Array
                (
                    [1] => 
                    [2] => 
                    [3] => 
                    [4] => 
                    [5] => 
                )

        )

)

下一步应该是填充所述新数组结构:

// populating the structured array
$week = 0;
foreach ($array as $val) {
    if (empty($array2[$val['id']][$week][$val['day']])) {
        $array2[$val['id']][$week][$val['day']] = $val['amount']; // set the appropriate day -> amount value
    } else {
        // but if there are two mondays, push the next monday to next week
        $num_batch = ceil($num_group_type[$val['id']] / $num_days_per_week);
        for ($next_possible_week = ($week + 1); $next_possible_week <= $num_batch; $j++) {
            if (empty($array2[$val['id']][$next_possible_week][$val['day']])) {
                // if possible to fill the next, fill and stop
                $array2[$service][$next_possible_week][$val['day']] = $val['amount'];
                break;
            }
        }

    }
}

基本上它只是将值从$array 放入/转移到新结构的$array2

如果没有为该 day amount 密钥对设置值,请设置它。如果已设置,请将其推送到下一个可用周。

之后,您将在适当的位置获得具有正确值的结构。下一步只是建立标记。样式由您决定,但最简单的方法是像以前一样使用 table 标签。

echo '<table border=1>';
echo '<tr><th>Table #2</th><th>Monday</th><th>Tuesday</th><th>Wednesday</th><th>Thursday</th><th>Friday</th></tr>';
foreach ($array2 as $service => $weeks) {
    foreach ($weeks as $week) {
        echo "<tr class=\"service{$service}\">";
        echo "<td>Service #{$service}</td>";
        foreach ($week as $day => $amount) {
            $amount = empty($amount) ? '&nbsp;' : $amount;
            echo "<td>{$amount}</td>";
        }
    }
    echo '</tr>';
}
echo '</table>';

这是你可以玩的小提琴:

https://www.tehplayground.com/o96tPV6rAwffsUPn

【讨论】:

  • 完美的 tnxx 男人!!
  • 当您在数组中添加此数据时,它会中断:["id"=>1,"day"=>"1","amount"=>90]
  • 错误在这里:$array2[$service][$next_possible_week][$val['day']] = $val['amount'];正确:$array2[$val['id']][$next_possible_week][$val['day']] = $val['amount'];
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-10
相关资源
最近更新 更多