如何在同一阵列中保存切换案例?

时间:2016-02-01 08:31:19

标签: php arrays

我需要在数组中保存一些数字。数字是 用户在我的网站上的持续时间。

喜欢:

duration        quantity
0-30 sek        50
30sek-2min      100
....

因此我的网页上有50个用户约为0-30 sek。

现在我有这个数组$ visit_length与此输出:

Array
(
    [0] => 3636
    [1] => 3637
    [2] => 3
    [3] => 40
    [4] => 9
)

这是用户在我的网站上的持续时间

现在我想做一个新的数组。在这个新阵列之前,我需要一个foreach和一个if / else或switch / case

像这样:

$dauer_result = [];
        foreach ($visit_lenght as $lenght) {
            switch ($lenght) {
                case $lenght < 30:
                    $dauer_result = count($lenght);
                    continue;
                case $lenght > 30 and $lenght < 120:
                    $dauer_result = count($lenght);
                    continue;
                case $lenght > 120 and $lenght < 300:
                    $dauer_result = count($lenght);
                    continue;
                case $lenght > 300 and $lenght < 900:
                    $dauer_result = count($lenght);
                    continue;
                case $lenght > 900 and $lenght < 1800:
                    $dauer_result = count($lenght);
                    continue;
                case $lenght > 1800 and $lenght < 3600:
                    $dauer_result = count($lenght);
                    continue;
                case $lenght > 3600:
                    $dauer_result = count($lenght);
            }
        }

当然这表示如果$ length超过'秒'的数量&#39;而不是在新数组$ dauer_length

中保存count($ length)的结果

例如:$ visit_length变量的前两个键是3636和3637

这意味着他们处于最后一种情况。所以$ dauer_result应该写成两个,因为它们都高于3600.

也许你们中的某个人有另一种更好的方式。 好吧,我希望你能理解我想说的话......我的英语并不是最好的......

感谢您的帮助:)

---------------------更新:-------------------

解决代码:

$dauer_result = array('<30'=>0, '30-120'=>0, '120-300'=>0, '300-900'=>0, '900-1800'=>0, '1800-3600'=>0, '>3600'=>0);
        foreach ($visit_lenght as $lenght) {
            switch ($lenght) {
                case $lenght < 30:
                    $dauer_result['<30'] +=1 ;
                    continue;
                case $lenght > 30 and $lenght < 120:
                    $dauer_result['30-120'] +=1 ;
                    continue;
                case $lenght > 120 and $lenght < 300:
                    $dauer_result['120-300'] +=1 ;
                    continue;
                case $lenght > 300 and $lenght < 900:
                    $dauer_result['300-900'] +=1 ;
                    continue;
                case $lenght > 900 and $lenght < 1800:
                    $dauer_result['900-1800'] +=1 ;
                    continue;
                case $lenght > 1800 and $lenght < 3600:
                    $dauer_result['1800-3600'] +=1 ;
                    continue;
                case $lenght 3600:
                    $dauer_result['>3600'] +=1 ;
                    continue;
            }
        }

2 个答案:

答案 0 :(得分:1)

我认为你在寻找

$dauer_result = array('<30'=>0, '30-120'=>0, '120-300'=>0, '300-900'=>0, '900-1800'=>0, '1800-3600'=>0, '>3600'=>0);
        foreach ($visit_lenght as $lenght) {
            switch ($lenght) {
                case $lenght < 30:
                    $dauer_result['<30'] +=1 ;
                    continue;
                case $lenght > 30 and $lenght < 120:
                    $dauer_result['30-120'] +=1 ;
                    continue;
                case $lenght > 120 and $lenght < 300:
                    $dauer_result['120-300'] +=1 ;
                    continue;
                case $lenght > 300 and $lenght < 900:
                    $dauer_result['300-900'] +=1 ;
                    continue;
                case $lenght > 900 and $lenght < 1800:
                    $dauer_result['900-1800'] +=1 ;
                    continue;
                case $lenght > 1800 and $lenght < 3600:
                    $dauer_result['1800-3600'] +=1 ;
                    continue;
                case $lenght 3600:
                    $dauer_result['>3600'] +=1 ;
                    continue;
            }
        }

答案 1 :(得分:0)

使用array_filter可以轻松完成此操作,$visit_length = array(3636, 3637, 3, 40, 9 ); function length_filter($var) { return ($var > 3600); } $result = array_filter($visit_length, 'length_filter'); 使用自定义函数过滤数组中的值。一个例子如下:

$result

变量3600现在包含一个数组,该数组只包含高于{user.name} has no submitted posts."的值。

相关问题