在阵列计数值

编程入门 行业动态 更新时间:2024-10-17 13:28:49
本文介绍了在阵列计数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想从这个数组获得值,并计算它们。比方说,我们有阿姆斯特丹,我想算值[41,21,43]一起,把它们放在一个HTML表格。问题是,这些值有时会错过,你可以看到下面。我怎样才能做到这一点?

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 )

这是我已经尝试过:

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.

如何获得阵列输出HTML表格

推荐答案

您需要循环在每块标题的另一个层次。

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>"; } }

更多推荐

在阵列计数值

本文发布于:2023-10-28 05:04:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535605.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:阵列   数值

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!