php 对子数组元素计数并对深层子数组元素求和

but5z9lq  于 2023-01-08  发布在  PHP
关注(0)|答案(1)|浏览(127)

我使用的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项(errorCode、result和data)的计数,但我无法确定如何仅关注data
我需要的计数值是4,总值是3542.97

p5cysglq

p5cysglq1#

对于计数,您需要:

count($array['data']);

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

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

***参考:-***PHP: array_column - Manual

相关问题