代码之家  ›  专栏  ›  技术社区  ›  user982124

PHP解析数组并获取计数

  •  0
  • user982124  · 技术社区  · 7 年前

    我正在使用一个API,该API返回一个JSON对象,然后使用

    我需要数一数所有的 [data] 然后生成所有 [Total] 每个中包含的值

    这是我的阵列:-

    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
                    )
    
            )
    
    )
    

    我尝试过:

    count($array)
    

    它返回 3 我可以看到父数组中的3个项目(错误代码、结果和数据)的计数,但我不知道如何专注于 data 只有

    我想要的计数值是 4 总价值为 3542.97 .

    1 回复  |  直到 7 年前
        1
  •  1
  •   Death-is-the-real-truth    7 年前

    对于count,您需要执行以下操作:-

    count($array['data']);
    

    为了得到所有的总和 Total 索引值:-

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

    联合收割机输出:- https://eval.in/913897

    参考:- PHP: array_column - Manual