PHP多维数组如果两个项匹配,则将相应的项相加并返回新数组
我有一个数组,返回如下:PHP多维数组如果两个项匹配,则将相应的项相加并返回新数组,php,arrays,sorting,multidimensional-array,Php,Arrays,Sorting,Multidimensional Array,我有一个数组,返回如下: array(5) { [0]=> array(2) { ["order_id"]=> string(3) "257" ["price"]=> string(7) "20.3600" } [1]=> array(2) { ["order_id"]=> string(3) "256" ["price"]=> string(7) "20.5500" }
array(5) {
[0]=>
array(2) {
["order_id"]=>
string(3) "257"
["price"]=>
string(7) "20.3600"
}
[1]=>
array(2) {
["order_id"]=>
string(3) "256"
["price"]=>
string(7) "20.5500"
}
[2]=>
array(2) {
["order_id"]=>
string(3) "255"
["price"]=>
string(7) "30.0000"
}
[3]=>
array(2) {
["order_id"]=>
string(3) "255"
["price"]=>
string(7) "22.3800"
}
[4]=>
array(2) {
["order_id"]=>
string(3) "254"
["price"]=>
string(7) "20.6300"
}
}
最后我想说的是:
array(4) {
[0]=>
array(2) {
["order_id"]=>
string(3) "257"
["price"]=>
string(7) "20.3600"
}
[1]=>
array(2) {
["order_id"]=>
string(3) "256"
["price"]=>
string(7) "20.5500"
}
[2]=>
array(2) {
["order_id"]=>
string(3) "255"
["price"]=>
string(7) "52.3800"
}
[3]=>
array(2) {
["order_id"]=>
string(3) "254"
["price"]=>
string(7) "20.6300"
}
}
逻辑是,如果数组包含匹配的订单号,则将这些订单号的价格相加,并返回一个新数组,其中包含唯一的订单id和添加的价格if以及一个订单id
因此,在本例中,由于有两个订单号为255的订单号,其价格分别为22.38和30,因此新数组中的订单号为255的订单号应该只有一个项目,相关价格为52.38
目前,我已经能够使用此函数仅返回唯一ID:
function super_unique($array,$key) {
$temp_array = array();
foreach ($array as &$v) {
if (!isset($temp_array[$v[$key]]))
$temp_array[$v[$key]] =& $v;
}
$array = array_values($temp_array);
return $array;
}
为了将价格相加,我尝试了类似的方法,但是我得到了操作数错误:
function super_unique_addition($array,$key,$total) {
$temp_array = array();
foreach ($array as &$v) {
if (!isset($temp_array[$v[$key]])) {
$temp_array[$v[$key]] =& $v;
$temp_array[$v[$total]] += $v;
}
}
$array = array_values($temp_array);
return $array;
}
非常感谢对此的任何见解!提前感谢您的帮助 只需按订单价格分组即可减少您的物品:
$newArray = array_reduce($array, function($memo, $item){
$memo[$item['order_id']] += $item['price'];
}, [])
编辑:请注意,您可以在查询时更好地对数据进行分组/求和。可能:
<?php
$your_array = array( ... );
# Loop array
for( $i=0; $i<count($your_array); $i++ )
{
# Loop array again for each item in it
for( $j=0; $j<count($your_array; $j++) )
{
# If the order IDs are the same, add the prices and make the $j array values null
if( $your_array[$i]["order_id"] === $your_array[$j]["order_id"] )
{
$your_array[$i]["price"] += $your_array[$j]["price"];
$your_array[$j]["order_id"] = null;
$your_array[$j]["price"] = null;
}
}
}
# array_filter should remove the null values
$final_array = array_filter($your_array);
?>
您可以在这一个上使用普通的ol'foreach,并在一个新数组上构建它。考虑这个例子:
$values = array(
0 => array('order_id' => '257', 'price' => '20.3600'),
1 => array('order_id' => '256', 'price' => '20.5500'),
2 => array('order_id' => '255', 'price' => '30.0000'),
3 => array('order_id' => '255', 'price' => '22.3800'),
4 => array('order_id' => '254', 'price' => '20.6300'),
);
$new_values = array();
foreach($values as $key => $value) {
$new_values[$value['order_id']]['order_id'] = $value['order_id'];
// initialize price
if(!isset($new_values[$value['order_id']]['price'])) $new_values[$value['order_id']]['price'] = 0;
$new_values[$value['order_id']]['price'] += $value['price'];
}
$new_values = array_values($new_values); // reindex the array
print_r($new_values); // print
$memo不太可能包含名为$item['order_id']的索引:
function _new_array()
{
$temp1 = [];
foreach (func_get_args() as $arg)
{
if (!is_array($arg)) continue;
foreach ($arg as $val)
{
if (isset($temp1[$val['order_id']]))
$temp1[$val['order_id']] += $val['price'];
else
$temp1[$val['order_id']] = $val['price'];
}
}
$result = [];
foreach ($temp1 as $k => $v) $result[] = ['order_id' => $k, 'price' => $v];
return $result;
}
$result = _new_array($array1, $array2, $array3);