按特定键计算值

时间:2016-06-08 07:49:03

标签: php arrays multidimensional-array

问题

我有一个棘手的问题,即只有当密钥amount等于name_pay时,如何计算密钥Click的值。我编写的PHP脚本只计算数组中的数量。 我试图将array_key_exists更改为array_count_values()但这是一个错误。

阵列

Array
(
[2016-05-31 00:00:00] => Array
    (
        [Основной долг] => Array
            (
                [0] => Array
                    (
                        [type_pay] => 0
                        [oper_type] => 4
                        [name_pay] => CLICK
                        [name_oper] => Основной долг
                        [time_pay] => 2016-05-31 00:00:00
                        [amount] => 1971022
                    )

                [1] => Array
                    (
                        [type_pay] => 1
                        [oper_type] => 4
                        [name_pay] => Наличные
                        [name_oper] => Основной долг
                        [time_pay] => 2016-05-31 00:00:00
                        [amount] => 564550
                    )

                [2] => Array
                    (
                        [type_pay] => 2
                        [oper_type] => 4
                        [name_pay] => Терминал
                        [name_oper] => Основной долг
                        [time_pay] => 2016-05-31 00:00:00
                        [amount] => 122714
                    )

            )

        [Депозит] => Array
            (
                [0] => Array
                    (
                        [type_pay] => 1
                        [oper_type] => 3
                        [name_pay] => Наличные
                        [name_oper] => Депозит
                        [time_pay] => 2016-05-31 00:00:00
                        [amount] => 1175942
                    )

                [1] => Array
                    (
                        [type_pay] => 2
                        [oper_type] => 3
                        [name_pay] => Терминал
                        [name_oper] => Депозит
                        [time_pay] => 2016-05-31 00:00:00
                        [amount] => 1316410
                    )

            )

        [] => Array
            (
                [0] => Array
                    (
                        [type_pay] => 100
                        [oper_type] => 
                        [name_pay] => Терминал
                        [name_oper] => 
                        [time_pay] => 2016-05-31 00:00:00
                        [amount] => 843795
                    )

            )

        [Итого] => Array
            (
                [0] => Array
                    (
                        [click_count] => test
                        [bill_count] => test
                        [terminal_count] => test
                        [time_pay] => 2016-05-31
                        [count_all] => test
                        [name_oper] => Итого
                    )

            )

    )

 )

PHP

$click = 0;
    foreach($data["reports"][1] as $v) {
        if(array_key_exists('amount', $v)) $click++;
    }

1 个答案:

答案 0 :(得分:0)

这是你想要的吗?

$click = 0;
foreach($data["reports"][1] as $v) {
    $click += (isset($v['name_pay']) && $v['name_pay'] == 'CLICK')? $v['amount']:0;
}

我不确定你的代码中是什么$data["reports"][1]但是因为那是你在迭代的东西,我也在使用它。