PHP 如果对象在数组中具有相同的键,同时携带剩余数据不变,则求和值
PHP Sum values if the object has the same key in a array whilst carrying the remaining data unchanged
Note: The following question is similar to this question, but I need more context for my answer.
我有一个像
这样的数组
Array
(
[0] => Array
(
[tax_name] => VAT
[tax_amount] => 3.000
[tax_type] => P
[tax_price] => 15.44
[price_includes_tax] => Y
)
[1] => Array
(
[tax_name] => VAT
[tax_amount] => 3.000
[tax_type] => P
[tax_price] => 59.68
[price_includes_tax] => Y
)
[2] => Array
(
[tax_name] => VAT
[tax_amount] => 3.000
[tax_type] => P
[tax_price] => 1.86
[price_includes_tax] => Y
)
[3] => Array
(
[tax_name] => GST
[tax_amount] => 3.300
[tax_type] => P
[tax_price] => 2.11
[price_includes_tax] => N
)
)
我想要这样的输出
Array
(
[0] => Array
(
[tax_name] => VAT
[tax_amount] => 3.000
[tax_type] => P
[tax_price] => 76.98
[price_includes_tax] => Y
)
[1] => Array
(
[tax_name] => GST
[tax_amount] => 3.300
[tax_type] => P
[tax_price] => 2.11
[price_includes_tax] => N
)
)
下面的代码部分解决了我的问题
$result = [];
array_walk($_tax_array, function($item) use (&$result) {
if (!isset($result[$item['tax_name']])) {
$result[$item['tax_name']] = 0;
}
$result[$item['tax_name']] += $item['tax_price'];
});
$resultFormated = [];
foreach ($result as $key => $value) {
$resultFormated[] = ['tax_name' => $key, 'tax_price' => $value];
}
print_r($resultFormated);
如果我 运行 这段代码,我得到的输出是
Array
(
[0] => Array
(
[tax_name] => VAT
[tax_price] => 76.98
)
[1] => Array
(
[tax_name] => GST
[tax_price] => 2.11
)
)
我的问题是我不知道如何在不影响结果的情况下携带 tax_amount
、tax_type
和 price_includes_tax
等剩余数据。
经过一段时间的尝试,我终于找到了答案。答案在这里
$tax_array = array();
foreach ($_tax_array as $array) {
$type= false;
foreach ($tax_array as $pos => $temp_array) {
if(
$temp_array['tax_name'] == $array['tax_name']){
$tax_array[$pos]['tax_price'] += $array['tax_price'];
$type= true;
}
}
if(!$type) {
$tax_array[] = array(
'tax_name' => $array['tax_name'],
'tax_type' => $array['tax_type'],
'tax_price' => $array['tax_price'],
'tax_amount' => $array['tax_amount'],
'price_includes_tax' => $array['price_includes_tax'],
);
}
}
print_r($tax_array);
这段代码解决了我的问题。此代码输出
Array
(
[0] => Array
(
[tax_name] => VAT
[tax_type] => P
[tax_price] => 76.98
[tax_amount] => 3.000
[price_includes_tax] => Y
)
[1] => Array
(
[tax_name] => GST
[tax_type] => P
[tax_price] => 2.11
[tax_amount] => 3.300
[price_includes_tax] => N
)
Note: The following question is similar to this question, but I need more context for my answer.
我有一个像
这样的数组Array
(
[0] => Array
(
[tax_name] => VAT
[tax_amount] => 3.000
[tax_type] => P
[tax_price] => 15.44
[price_includes_tax] => Y
)
[1] => Array
(
[tax_name] => VAT
[tax_amount] => 3.000
[tax_type] => P
[tax_price] => 59.68
[price_includes_tax] => Y
)
[2] => Array
(
[tax_name] => VAT
[tax_amount] => 3.000
[tax_type] => P
[tax_price] => 1.86
[price_includes_tax] => Y
)
[3] => Array
(
[tax_name] => GST
[tax_amount] => 3.300
[tax_type] => P
[tax_price] => 2.11
[price_includes_tax] => N
)
)
我想要这样的输出
Array
(
[0] => Array
(
[tax_name] => VAT
[tax_amount] => 3.000
[tax_type] => P
[tax_price] => 76.98
[price_includes_tax] => Y
)
[1] => Array
(
[tax_name] => GST
[tax_amount] => 3.300
[tax_type] => P
[tax_price] => 2.11
[price_includes_tax] => N
)
)
下面的代码部分解决了我的问题
$result = [];
array_walk($_tax_array, function($item) use (&$result) {
if (!isset($result[$item['tax_name']])) {
$result[$item['tax_name']] = 0;
}
$result[$item['tax_name']] += $item['tax_price'];
});
$resultFormated = [];
foreach ($result as $key => $value) {
$resultFormated[] = ['tax_name' => $key, 'tax_price' => $value];
}
print_r($resultFormated);
如果我 运行 这段代码,我得到的输出是
Array
(
[0] => Array
(
[tax_name] => VAT
[tax_price] => 76.98
)
[1] => Array
(
[tax_name] => GST
[tax_price] => 2.11
)
)
我的问题是我不知道如何在不影响结果的情况下携带 tax_amount
、tax_type
和 price_includes_tax
等剩余数据。
经过一段时间的尝试,我终于找到了答案。答案在这里
$tax_array = array();
foreach ($_tax_array as $array) {
$type= false;
foreach ($tax_array as $pos => $temp_array) {
if(
$temp_array['tax_name'] == $array['tax_name']){
$tax_array[$pos]['tax_price'] += $array['tax_price'];
$type= true;
}
}
if(!$type) {
$tax_array[] = array(
'tax_name' => $array['tax_name'],
'tax_type' => $array['tax_type'],
'tax_price' => $array['tax_price'],
'tax_amount' => $array['tax_amount'],
'price_includes_tax' => $array['price_includes_tax'],
);
}
}
print_r($tax_array);
这段代码解决了我的问题。此代码输出
Array
(
[0] => Array
(
[tax_name] => VAT
[tax_type] => P
[tax_price] => 76.98
[tax_amount] => 3.000
[price_includes_tax] => Y
)
[1] => Array
(
[tax_name] => GST
[tax_type] => P
[tax_price] => 2.11
[tax_amount] => 3.300
[price_includes_tax] => N
)