duandong7980 2016-01-08 16:05
浏览 20
已采纳

计算数组中的值[关闭]

I am trying to get values from this array and counting them. Let's say we have Amsterdam and I would like to count value [41, 21, 43] together and put them in a html table. The problem is that the values sometimes miss as you can see below. How can I achieve this?

Array
(
    [Amsterdam] => Array
        (
            [41] => 2
            [21] => 91
            [43] => 16
            [42] => 2
            [20] => 30
            [4] => 4
            [70] => 3
            [84] => 8
            [46] => 4
            [45] => 5
            [999] => 26
            [47] => 2
            [3] => 8
            [44] => 1
            [40] => 1
            [93] => 5
            [56] => 3
            [61] => 3
            [79] => 3
            [48] => 2
            [50] => 5
            [10] => 10
            [52] => 2
            [120] => 1
            [95] => 1
            [1] => 64
            [90] => 4
            [100] => 2
            [101] => 1
        )

    [Rotterdam] => Array
        (
            [21] => 42
            [41] => 2
            [42] => 2
            [46] => 1
            [47] => 2
            [43] => 4
            [45] => 3
            [4] => 1
            [3] => 19
            [84] => 1
            [12] => 1
            [20] => 14
            [40] => 1
            [48] => 6
            [61] => 1
            [52] => 1
            [10] => 4
            [1] => 23
            [90] => 2
        )

    [Spaarnwoude] => Array
        (
            [21] => 2
        )

This is what I already tried:

  foreach ($headings as $h) {
        echo "<th>$h</th>";
    }
    echo '</tr>';

    foreach($cities as $cityname => $city) { 
        echo '<tr>';
        echo "<td>$cityname</td>";
        foreach (array_chunk($headings, 3) as $h) {
            echo '<td>' . (isset($city[$h]) ? $city[$h] : '0') . '</td>';
        }    
        echo '</tr>';
    }

    echo '</table>';

For further information you can check this link.

How to get array output in html table

  • 写回答

1条回答 默认 最新

  • doulu6314 2016-01-08 21:32
    关注

    You need another level of looping for each heading in the chunks.

    $chunked_headings = array_chunk($headings, 3);
    echo '<tr>';
    foreach ($chunked_headings as $heading_group) {
        echo '<th>' . implode(', ', $heading_group) . '</th>';
    }
    echo '</tr>';
    
    foreach ($cities as $cityname => $city) {
        echo '<tr>';
        echo "<td>$cityname</td>";
        foreach ($chunked_headings as $heading_group) {
            $total = 0;
            foreach ($heading_group as $h) {
                if (isset($city[$h])) {
                    $total += $city[$h];
                }
            }
            echo "<td>$total</td>";
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c