基于 PHP 的条件计算数组元素数量

huangapple go评论58阅读模式
英文:

Count array elements based on criterias PHP

问题

$array = $result['rawSumSnowDepth'];

foreach ($array as $key => $items) {
    if ($items['fSnowDepth'] >= 1 && $items['fSnowDepth'] < 3) {
        $value          = $items['fMonth'];
        $output[$value] = ($output[$value] ?? 0) + 1;

        for ($x = 0; $x <= 11; $x++) {
            if ($x + 1 == $items['fMonth']) {
                $result['snDaysOverAmt'][$x] = array($output[$value]);
            } elseif (empty($result['snDaysOverAmt'][$x])) {
                $result['snDaysOverAmt'][$x] = array($output[$value] => "&nbsp;");
            }
        }
    }
}

if (isset($result['snDaysOverAmt'])) {
    foreach ($result['snDaysOverAmt'] as $amounts => $amount) {
        if ($amount) {
            echo '<td>' . implode($amount) . '</td>';
        }
    }
}
英文:

Looking for help in counting elements in a php array meeting certain criteria and getting them to properly display in html table.

I have this array named $Array

Array
(
    [0] =&gt; Array
        (
            [fMonth] =&gt; 12
            [fSnowDepth] =&gt; 0.2
        )

    [1] =&gt; Array
        (
            [fMonth] =&gt; 12
            [fSnowDepth] =&gt; 3.7
        )

    [2] =&gt; Array
        (
            [fMonth] =&gt; 12
            [fSnowDepth] =&gt; 1
        )

    [3] =&gt; Array
        (
            [fMonth] =&gt; 01
            [fSnowDepth] =&gt; 1
        )

    [4] =&gt; Array
        (
            [fMonth] =&gt; 01
            [fSnowDepth] =&gt; 0.5
        )

    [5] =&gt; Array
        (
            [fMonth] =&gt; 01
            [fSnowDepth] =&gt; 4.5
        )

    [6] =&gt; Array
        (
            [fMonth] =&gt; 01
            [fSnowDepth] =&gt; 1.3
        )

)

What I'm trying to do is count the months which meet conditions such as fSnowDepth >= 1 and < 3, fSnowDepth >= 3 and < 5, etc. and place in html table. I'm expecting this with (blank) for the months with no count:

               | Jan  | Feb  | Mar  | Apr  | May  | Jun  | Jul  | Aug  | Sep  | Oct  | Nov  | Dec
SD &gt;=1 and &lt; 3 |  2   |      |      |      |      |      |      |      |      |      |      |  1
SD &gt;=3 and &lt; 5 |  1   |      |      |      |      |      |      |      |      |      |      |  1
... etc depths |      |      |      |      |      |      |      |      |      |      |      |     

The code I have is:

$array = $result[&#39;rawSumSnowDepth&#39;];

foreach ($array as $key =&gt; $items) {
    if ($items[&#39;fSnowDepth&#39;] &gt;= 1 &amp;&amp; $items[&#39;fSnowDepth&#39;] &lt; 3) {
        $value          = $items[&#39;fMonth&#39;];
        $output[$value] = ($output[$value] ?? 0) + 1;

            for ($x = 0; $x &lt;= 11; $x++) {
                if ($x + 1 == $items[&#39;fMonth&#39;]) {
                    $result[&#39;snDaysOverAmt&#39;][$x] = array($output[$value]);
                } elseif (empty($result[&#39;snDaysOverAmt&#39;][$x])) {
                    $result[&#39;snDaysOverAmt&#39;][$x] = array($output[$value] =&gt; &quot;&amp;nbsp;&quot;);
                }
            }
        }
    }

if (isset($result[&#39;snDaysOverAmt&#39;])) {
    foreach ($result[&#39;snDaysOverAmt&#39;] as $amounts =&gt; $amount) {
        if ($amount) {
            echo &#39;&lt;td&gt;&#39; . implode($amount) . &#39;&lt;/td&gt;&#39;;
        }
    }
}

This code works like a charm for the first row of snow depths >= 1 and < 3 but when I run the code again for the next row (>= 3 and < 5) and I get what appears to be a doubling of the first row.

Is there another way to do this so I can include different snow depth counts AND is there a more concise way of doing this? I'm still a PHP rookie so any help would be appreciated.

答案1

得分: -1

$output = [];

$result['snDaysOverAmt'] = [];

这些代码需要放在上述问题中提供的代码的最后一个闭括号之后。

英文:

Thanks to @Barmar this was all that was needed:

$output = [];

$result['snDaysOverAmt'] = [];

these were placed after the last closing bracket in the code provided in the question above.

huangapple
  • 本文由 发表于 2023年1月9日 03:09:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050567.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定