drruhc4944 2017-12-07 04:05
浏览 33
已采纳

PHP解析数组和获取计数

I'm working with an API that returns a JSON object which I then decode into an array using

I need to get a count of all the [data] items in the array, and then generate a total of all the [Total] values contained in each

Here's my array:-

Array
(
    [errorCode] => 0
    [result] => OK
    [data] => Array
        (
            [0] => Array
                (
                    [fieldData] => Array
                        (
                            [Due Date] => 11/30/2017
                            [Date] => 11/30/2017
                            [Total] => 128.97
                            [Customers::Company] => A B C Lock & Key
                            [Status] => Paid
                            [Date Payment] => 11/30/2017
                        )

                    [portalData] => Array
                        (
                        )

                    [recordId] => 1
                    [modId] => 4
                )

            [1] => Array
                (
                    [fieldData] => Array
                        (
                            [Due Date] => 12/01/2017
                            [Date] => 12/01/2017
                            [Total] => 256
                            [Customers::Company] => Kim Peacock Beringhause
                            [Status] => Paid
                            [Date Payment] => 12/01/2017
                        )

                    [portalData] => Array
                        (
                        )

                    [recordId] => 2
                    [modId] => 3
                )

            [2] => Array
                (
                    [fieldData] => Array
                        (
                            [Due Date] => 11/30/2017
                            [Date] => 11/30/2017
                            [Total] => 1880
                            [Customers::Company] => Norton, Robert L Esq
                            [Status] => Unpaid Overdue
                            [Date Payment] => 
                        )

                    [portalData] => Array
                        (
                        )

                    [recordId] => 3
                    [modId] => 0
                )



            [3] => Array
                (
                    [fieldData] => Array
                        (
                            [Due Date] => 12/22/2017
                            [Date] => 12/22/2017
                            [Total] => 1278
                            [Customers::Company] => Shapiro, Mark R Esq
                            [Status] => Unpaid
                            [Date Payment] => 
                        )

                    [portalData] => Array
                        (
                        )

                    [recordId] => 10
                    [modId] => 1
                )

        )

)

I've tried:

count($array)

which returns 3 which I can see are the count of the 3 items in that parent array (errorCode, result and data), but I can't work out how to focus on the data only.

The count value I'm after is 4 and the total value is 3542.97.

  • 写回答

1条回答 默认 最新

  • duankezong4064 2017-12-07 04:07
    关注

    For count you need to do:-

    count($array['data']);
    

    And for getting sum of all Total index values:-

    echo array_sum(array_column(array_column($array['data'],'fieldData'),'Total'));
    

    Combine Output:- https://eval.in/913897

    Reference:- PHP: array_column - Manual

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记