php 如何计算数组中对象的值?

00jrzges  于 2023-04-28  发布在  PHP
关注(0)|答案(4)|浏览(125)

用这个:

$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;
}

我得到:

[
  {
    "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
  }
]

但我需要$total101, 102, 103的和。101, 102, 103是动态的,因此这些键将不同。
我试图排除timeSummary,但似乎不起作用:

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

a11xaf1n1#

也许是这样的:

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

yvgpqqbh

yvgpqqbh2#

$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
);
oug3syen

oug3syen3#

最后我取消了timeSumary的设置,设置了Total,然后再次设置了timeSummary

$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'];
}
xzv2uavs

xzv2uavs4#

不要将数据与元数据和聚合值保持在同一级别。将数据放在单独的列表属性中,如下所示:

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

检查online

相关问题