函数内部的foreach没有输出值

时间:2016-05-29 13:15:16

标签: php arrays function

我在函数中使用foreach但是,我无法从中输出正确的值。

我有一个由函数

处理的数组
//this is only a small part of it because it is very large
Array
(
    [2016-05-02] => Array
        (
            [grup_1] => Array
                (
                    [luce] => 4
                    [ctr_ok] => 3
                    [ctr_tot] => 7
                    [ctr_ko] => 4
                    [gas] => 3
                    [ore] => 30.5
                )

            [grup_2] => Array
                (
                    [luce] => 3
                    [ctr_ko] => 4
                    [ctr_tot] => 6
                    [gas] => 3
                    [ctr_ok] => 2
                    [ore] => 47
                )

            [grup_3] => Array
                (
                    [luce] => 6
                    [ctr_ko] => 1
                    [ctr_tot] => 8
                    [ctr_gia_cliente] => 1
                    [ctr_ok] => 6
                    [gas] => 2
                    [ore] => 24
                )

            [grup_4] => Array
                (
                    [luce] => 4
                    [ctr_ok] => 4
                    [ctr_tot] => 8
                    [gas] => 4
                    [ctr_ko] => 4
                    [ore] => 30
                )

            [grup_5] => Array
                (
                    [luce] => 9
                    [ctr_ko] => 11
                    [ctr_tot] => 17
                    [gas] => 8
                    [ctr_ok] => 6
                    [ore] => 35
                )

            [grup_6] => Array
                (
                    [luce] => 1
                    [ctr_ok] => 2
                    [ctr_tot] => 2
                    [gas] => 1
                    [ore] => 36
                )

            [grup_7] => Array
                (
                    [luce] => 5
                    [ctr_ko] => 1
                    [ctr_tot] => 7
                    [ctr_ok] => 6
                    [gas] => 2
                    [ore] => 22
                )

        )

    [2016-05-03] => Array
        (
            [grup_1] => Array
                (
                    [luce] => 6
                    [ctr_ok] => 6
                    [ctr_tot] => 10
                    [gas] => 4
                    [ctr_ko] => 4
                    [ore] => 33.5
                )

            [grup_2] => Array
                (
                    [luce] => 6
                    [ctr_ok] => 4
                    [ctr_tot] => 8
                    [ctr_ko] => 2
                    [gas] => 2
                    [ctr_att_green] => 2
                    [ore] => 36
                )

            [grup_3] => Array
                (
                    [luce] => 6
                    [ctr_ok] => 4
                    [ctr_tot] => 9
                    [gas] => 3
                    [ctr_ko] => 5
                    [ore] => 36
                )

            [grup_4] => Array
                (
                    [luce] => 5
                    [ctr_ko] => 2
                    [ctr_tot] => 10
                    [gas] => 5
                    [ctr_ok] => 8
                    [ore] => 42
                )

            [grup_5] => Array
                (
                    [gas] => 2
                    [ctr_ok] => 3
                    [ctr_tot] => 3
                    [luce] => 1
                    [ore] => 23
                )

            [grup_6] => Array
                (
                    [luce] => 1
                    [ctr_ko] => 2
                    [ctr_tot] => 2
                    [gas] => 1
                    [ore] => 36
                )

            [grup_7] => Array
                (
                    [luce] => 2
                    [ctr_ok] => 1
                    [ctr_tot] => 3
                    [ctr_gia_cliente] => 2
                    [gas] => 1
                    [ore] => 27.3
                )

        )

这是收集ctr_tot密钥

之和的函数
function kontratat_tot($grup_name){
        $total = 0;
        foreach ($kontrata as $date => $grup){
            if($grup[$grup_name]['ctr_tot'] != 0){
                $total += $grup[$grup_name]['ctr_tot'];
            }
        }
    return $total;
}

在这里我调用函数

kontratat_tot("grup_1");

我一直在寻找过去3个小时为我的问题找到任何解决方案,但我已经卡住了,即使解决方案是从我的眼睛出来我也看不到它。

2 个答案:

答案 0 :(得分:1)

$kontratakontratat_tot函数的outside the scope。尝试将其指定为global,或传入您的函数。

function kontratat_tot($grup_name)
{
    global $kontrata;

    $total = 0;

    foreach( $kontrata as $date => $grup )
    {
        if($grup[$grup_name]['ctr_tot'] != 0)
        {
            $total += $grup[$grup_name]['ctr_tot'];
        }
    }

    return $total;
}

答案 1 :(得分:1)

您的函数正在尝试访问不属于当前范围但位于父范围内的$kontrata

我建议你将数据作为参数传递,定义全局变量并在函数内访问它将限制你使用相同的变量名,如果你想使用这个函数两次或更多。

function kontratat_tot($kontrata, $grup_name){
        $total = 0;
        foreach ($kontrata as $date => $grup){
            if($grup[$grup_name]['ctr_tot'] != 0){
                $total += $grup[$grup_name]['ctr_tot'];
            }
        }
    return $total;
}