vendredi 4 juin 2021

Sum Multi-dimensional PHP Array and append Totals back to Array [closed]

I have an PHP array like below (I got this using Laravel Query Builder)

array:3 [▼
  "" => array:4 [▼
    1 => 1727723894.4161
    2 => 1802681216.9919
    3 => 10152489.054398
    4 => 210222412.87713
  ]
  2 => array:4 [▼
    1 => 282077115.21716
    2 => 89886891.736643
    3 => 15110471.197525
    4 => 3296492.33976
  ]
  1 => array:4 [▼
    1 => 119691847.8251
    2 => 29205135.74572
    3 => 5241987.169531
    4 => 1071239.29433
  ]
]

I want to sum the array (I found array_sum to do sum) and append Total as new element back to array like this ( could not find a solution this),

array:3 [▼
  "" => array:4 [▼
    1 => 1727723894.4161
    2 => 1802681216.9919
    3 => 10152489.054398
    4 => 210222412.87713
    Total => 3750780013
  ]
  2 => array:4 [▼
    1 => 282077115.21716
    2 => 89886891.736643
    3 => 15110471.197525
    4 => 3296492.33976
    Total => 390370970.5
  ]
  1 => array:4 [▼
    1 => 119691847.8251
    2 => 29205135.74572
    3 => 5241987.169531
    4 => 1071239.29433
    Total => 155210210
  ]
]


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire