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 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败