skip to Main Content

With this:

$obj1 = [
    "101" => 18,
    "102" => 15,
    "103" => 23,
    "timeSummary" => "2023-04-17 00:00:00"
];
 
$obj2 = [
    "101" => 15,
    "102" => 13,
    "103" => 79,
    "timeSummary" => "2023-04-18 00:00:00"
];
$obj3 = [
    "101" => 50,
    "102" => 50,
    "103" => 62,
    "timeSummary" => "2023-04-19 00:00:00"
];
$testArray = [$obj1, $obj2, $obj3];

// add Total
$withTotal = [];
$total = 0;
foreach ($testArray as $key => $value) {
    array_push($withTotal, $value);
    $total += array_sum($value);
    $withTotal[$key]['total'] = $total;
}

I get:

[
  {
    "101": 18,
    "102": 15,
    "103": 23,
    "timeSummary": "2023-04-17 00:00:00",
    "total": 2079
  },
  {
    "101": 15,
    "102": 13,
    "103": 79,
    "timeSummary": "2023-04-18 00:00:00",
    "total": 4209
  },
  {
    "101": 50,
    "102": 50,
    "103": 62,
    "timeSummary": "2023-04-19 00:00:00",
    "total": 6394
  }
]

but I need the $total be the SUM of 101, 102, 103. 101, 102, 103 are dynamic so these keys will be different.

I try to exclude timeSummary but it doesn’t seem to work:

if (!$withTotal[$key]['timeSummary']) {
                $total += array_sum($value);
            }

4

Answers


  1. Chosen as BEST ANSWER

    I ended up unsetting timeSumary setting the Total and then setting timeSummary again.

    $withTotal = [];
    
    foreach ($testArray as $key => $value) {
        $total = 0;
        array_push($withTotal, $value);
        unset($withTotal[$key]['timeSummary']);
    
        $total += array_sum($withTotal[$key]);
        $withTotal[$key]['total'] = $total;
    
        $withTotal[$key]['timeSummary'] = $testArray[$key]['timeSummary'];
    }
    

  2. Do not keep the data on the same level as the metadata and the aggregated values. Put the data in a separate list property, as below:

    $obj1 = [
        "data" => [
            "101" => 18,
            "102" => 15,
            "103" => 23,
        ],
        "timeSummary" => "2023-04-17 00:00:00"
    ];
     
    $obj2 = [
        "data" => [
            "101" => 15,
            "102" => 13,
            "103" => 79,
        ],
        "timeSummary" => "2023-04-18 00:00:00"
    ];
    $obj3 = [
        "data" => [
            "101" => 50,
            "102" => 50,
            "103" => 62,
        ],
        "timeSummary" => "2023-04-19 00:00:00"
    ];
    $testArray = [$obj1, $obj2, $obj3];
    
    // add Total
    $withTotal = [];
    $total = 0;
    foreach ($testArray as $obj) {
        // compute the total for this object
        $obj['total'] = array_sum($obj['data']);
        // add it to the big total
        $total += $obj['total'];
        // put the updated object in a new array
        array_push($withTotal, $obj);
    }
    
    print_r($withTotal);
    

    Check it online.

    Login or Signup to reply.
  3. maybe something like this:

    <?php
        $obj1 = [
            "101" => 18,
            "102" => 15,
            "103" => 23,
            "timeSummary" => "2023-04-17 00:00:00"
        ];
         
        $obj2 = [
            "101" => 15,
            "102" => 13,
            "103" => 79,
            "timeSummary" => "2023-04-18 00:00:00"
        ];
        $obj3 = [
            "101" => 50,
            "102" => 50,
            "103" => 62,
            "timeSummary" => "2023-04-19 00:00:00"
        ];
        $testArray = [$obj1, $obj2, $obj3];
        foreach ($testArray as $key => $arr) {
            $sum = array_sum(array_filter($arr,"is_numeric"));
            $testArray[$key]['total'] = $sum;
        }
        print_r($testArray);
    

    https://onlinephp.io/c/8cec8

    Login or Signup to reply.
  4. $testArray = [
      [
        '101'         => 18,
        '102'         => 15,
        '103'         => 23,
        'timeSummary' => '2023-04-17 00:00:00'
      ],
      [
        '101'         => 15,
        '102'         => 13,
        '103'         => 79,
        'timeSummary' => '2023-04-18 00:00:00'
      ],
      [
        '101'         => 50,
        '102'         => 50,
        '103'         => 62,
        'timeSummary' => '2023-04-19 00:00:00'
      ]
    ];
    
    $result = array_map(
      static fn($obj) => $obj +
        [
          'total' => array_sum(
            array_filter(
              $obj,
              static fn($key) => $key !== 'timeSummary',
              ARRAY_FILTER_USE_KEY
            )
          )
        ]
      ,
      $testArray
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search