PHP 基于相同值和值之和的自定义数组合并

PHP custom array merge on bases of same value and sum of value

我有如下数组,

Array(
    [0] => Array(
        [account] => 251234567890,
        [price] => 83
    ) [1] => Array(
        [account] => 251234567890,
        [price] => 27
    ) [2] => Array(
        [account] => 251234564526,
        [price] => 180
    ) [3] => Array(
        [account] => 251234567890,
        [price] => 40
    )
)

现在我想合并具有相同 account 的数组及其总和 price.

我想要这样的输出数组,

Array(
    [251234567890] => Array(
        [account] => 251234567890,
        [price] => 150
    ) [251234564526] => Array(
        [account] => 251234564526,
        [price] => 180
    )
)

我试过这样,

$res = array();
$k = 0;
foreach ($to_account as $vals) {
  if (array_key_exists($vals['account'], $res)) {
    $res[$k]['price'] += $vals['price'];
    $res[$k]['account'] = $vals['account'];
    $k++;
  } else {
    $res[$k] = $vals;
    $k++;
  }
}

因为在输入数组中只有两个唯一的 account 存在所以输出数组应该是这两个帐户的价格之和

我在 python 中看到过类似的内容,但它希望对您有所帮助在 python 我想要它在 php 我希望有人能帮助我

谢谢

我会分两步完成,首先通过帐户输入密钥,然后转换为您想要的输出格式:

$data = [
  [
    'account' => 251234567890,
    'price' => 83
  ],
  [
    'account' => 251234567890,
    'price' => 27
  ],
  [
    'account' => 251234564526,
    'price' => 180
  ],
  [
    'account' => 251234567890,
    'price' => 40
  ],
];


$keyed = [];
foreach ($data as $item) {
  if (!isset($keyed[$item['account']])) {
    $keyed[$item['account']] = 0;
  }
  $keyed[$item['account']] += $item['price'];
}

$merged = [];
foreach ($keyed as $account => $price) {
  $merged[] = compact('account', 'price');
}

print_r($merged);

或者,更实用的解决方案(我喜欢,但有点难以理解):

$keyed = array_reduce($data, function ($carry, $item) {
  if (!isset($carry[$item['account']])) {
    $carry[$item['account']] = 0;
  }
  $carry[$item['account']] += $item['price'];
  return $carry;
}, []);

$merged = array_map(function ($account, $price) {
  return compact('account', 'price');
}, array_keys($keyed), $keyed);

@Jeroen 注意你的回答对我很有用,但我已经自己解决了,

$final_payment_array = array();

foreach ($to_account as $vals) {
     if (array_key_exists($vals['account'], $final_payment_array)) {
            $final_payment_array[$vals['account']]['price'] += $vals['price'];
            $final_payment_array[$vals['account']]['account'] = $vals['account'];
     } else {
            $final_payment_array[$vals['account']] = $vals;
       }
}

我认为这是 性能方面的最佳解决方案

谢谢

如果你尝试做类似的事情会怎么样:

$arr = array(
    0 => array(
        'account' => 251234567890,
        'price' => 83
    ), 1 => array(
        'account' => 251234567890,
        'price' => 27
    ), 2 =>array(
        'account' => 251234564526,
        'price' => 180
    ), 3 => array(
        'account' => 251234567890,
        'price' => 40
    )
);

foreach($arr as $key => $value) {
    $newArr[$value['account']][] = $value['price'];
}

foreach($newArr as $key => $value) {
    $finalArr[] = array('account'=>$key,'price'=>array_sum($value));
}

$finalArr:

Array ( [0] => Array ( 
[account] => 251234567890 
[price] => 150 ) 
[1] => Array ( 
[account] => 251234564526 
[price] => 180 ) )

通过分配临时键,您可以确定是否正在处理第一次出现的事件,然后使用适当的技术存储整个子数组或仅向子数组的 price 元素添加值.

代码:(Demo)

$to_account = [
  [ 'account' => 251234567890, 'price' => 83 ],
  [ 'account' => 251234567890, 'price' => 27 ],
  [ 'account' => 251234564526, 'price' => 180 ],
  [ 'account' => 251234567890, 'price' => 40 ]
];

foreach ($to_account as $row) {
    if (!isset($result[$row['account']])) {
        $result[$row['account']] = $row;
    } else {
        $result[$row['account']]['price'] += $row['price'];
        // imitate the above line if there was another column to sum
    }
}
var_export($result);

输出:

array (
  251234567890 => 
  array (
    'account' => 251234567890,
    'price' => 150,
  ),
  251234564526 => 
  array (
    'account' => 251234564526,
    'price' => 180,
  ),
)

此方法不会覆盖多余的帐户元素值。要重新索引输出数组,只需对其调用 array_values()

这是我的解决方案。你可以试试这个

   $myArray = array(

        0 => array(
            account => 251234567890,
            price => 83,
            ),

        1 => array(
            account => 251234567890,
            price => 27,
            ),
        2 => array(
            account => 251234564526,
            price => 180,
            ),
        3 => array(
            account => 251234567890,
            price => 40,
            ),
   );
$keyed = [];
foreach ($myArray as $item) {
  if (!isset($keyed[$item['account']])) {
    $keyed[$item['account']] = 0;
  }
  $keyed[$item['account']] += $item['price'];
}

$merged = [];
foreach ($keyed as $account => $price) {
  $merged[] = compact('account', 'price');
}

print_r($merged);

https://eval.in/506410