相同值的数组之和

时间:2014-04-26 12:51:23

标签: php arrays

我有这个数组:

Array (
  [0] => Array (
    [TaxeName] => TPS
    [TaxeAmount] => 7
    [Price] => 14
  )
  [1] => Array (
    [TaxeName] => TVQ
    [TaxeAmount] => 9.975
    [Price] => 10
  )
  [2] => Array (
    [TaxeName] => TVQ
    [TaxeAmount] => 9.975
    [Price] => 18
  )
)

我如何获得另一个阵列:
- 将TaxeNameTaxeAmount
分组 - 将金额加Price

像这样:

Array (
  [0] => Array (
    [TaxeName] => TPS
    [TaxeAmount] => 7
    [Price] => 14
  )
  [1] => Array (
    [TaxeName] => TVQ
    [TaxeAmount] => 9.975
    [Price] => 28
  )
)

5 个答案:

答案 0 :(得分:0)

这样的事情可能是:

$final = array();
foreach ($arr as $subarr)
{
    if (!isset($final[$subarr['TaxeName']]))
    {
        $final[$subarr['TaxeName']] = array('Price' => 0);
    }
    $final[$subarr['TaxeName']]['TaxeName'] = $subarr['TaxeAmount'];
    $final[$subarr['TaxeName']]['TaxeName'] = $subarr['TaxeName'];
    $final[$subarr['TaxeName']]['Price'] = $final[$subarr['TaxeName']]['Price'] + $subarr['Price'];
}

我们的想法是创建一个包含您需要的值的新数组,并在价格设置为0的情况下对其进行初始化,以便您可以在foreach中增加它

答案 1 :(得分:0)

你也可以这样做:

<?php

// Source
$myData = array(
    array(
        'TaxeName' => 'TPS',
        'TaxeAmount' => 7,
        'Price' => 14,
    ),
    array(
        'TaxeName' => 'TVQ',
        'TaxeAmount' => 9.975,
        'Price' => 10,
    ),
    array(
        'TaxeName' => 'TVQ',
        'TaxeAmount' => 9.975,
        'Price' => 18,
    )
);

// Helper Function
function parseData($data)
{
    $parsedData = array();

    if (is_array($data)) {
        foreach ($data as $dataItem) {
            if (!array_key_exists($dataItem['TaxeName'], $parsedData)) {
                $parsedData[$dataItem['TaxeName']] = array(
                    'TaxeName' => $dataItem['TaxeName'],
                    'TaxeAmount' => $dataItem['TaxeAmount'],
                    'Price' => floatval($dataItem['Price'])
                );
            } else {
                $parsedData[$dataItem['TaxeName']]['Price'] += floatval($dataItem['Price']);
            }
        }
    }

    return array_values($parsedData);
}

// Test
$parsed_data = parseData($myData);
var_dump($parsed_data);

?>

输出:

enter image description here

答案 2 :(得分:0)

可以使用嵌套的foreach

来完成
$original = array(.......); // your current array
$newArr = array();

foreach($original as $origVal){
    $exists = false;
    foreach($newArr as $key => $newVal){
        if($newVal['TaxeName'] == $origVal['TaxeName']){
            $newArr[$key]['Price'] += $origVal['Price'];
            $exists = true;
            break;
        }
    }

    if(!$exists){
        $newArr[] = $origVal;
    }
}

输出

Array
(
    [0] => Array
        (
            [TaxeName] => TPS
            [TaxeAmount] => 7
            [Price] => 14
        )

    [1] => Array
        (
            [TaxeName] => TVQ
            [TaxeAmount] => 9.975
            [Price] => 28
        )

)

答案 3 :(得分:0)

只需使用此功能,输入就是您的数组,输出是您需要的数组:

function calculate(array $input){
    $output = [];
    foreach ($input as $v){
        if (array_key_exists($v['TaxeName'], $output)){
            $output[$v['TaxeName']]['Price'] += $v['Price'];
        } else {
            $output[$v['TaxeName']] = $v;
        }
    }

    return array_values($output);
}

答案 4 :(得分:0)

我知道在SO上不欢迎使用外部类的解决方案。我显示此信息只是为了说明“将TaxeName TaxeAmount”分组的任务。

//use class tablearray from https://github.com/jspit-de/tableArray
$data = [
  ['TaxeName' => 'TPS', 'TaxeAmount' => 7, 'Price' => 14],
  ['TaxeName' => 'TVQ', 'TaxeAmount' => 9.975, 'Price' => 10],
  ['TaxeName' => 'TVQ', 'TaxeAmount' => 9.975, 'Price' => 18],
  ['TaxeName' => 'TVQ', 'TaxeAmount' => 19.975, 'Price' => 23]
];

$newData = tableArray::create($data)
  ->filterGroupSum('Price',['TaxeName','TaxeAmount'])
  ->fetchAll()
;

echo '<pre>';
var_export($newData);

输出:

array (
  0 => 
  array (
    'TaxeName' => 'TPS',
    'TaxeAmount' => 7,
    'Price' => 14,
  ),
  1 => 
  array (
    'TaxeName' => 'TVQ',
    'TaxeAmount' => 9.975,
    'Price' => 28,
  ),
  2 => 
  array (
    'TaxeName' => 'TVQ',
    'TaxeAmount' => 19.975,
    'Price' => 23,
  ),
)