PHP多维数组如果两个项匹配,则将相应的项添加到一起并返回新数组


PHP Multidemensional Array if two items match then add together a corresponding item and return new 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"
  }
  [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的新数组。

因此,在该示例中,由于存在两个相同的order_id(255(,价格分别为22.38和30,因此新数组应该只有一个order_id 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;
    }

非常感谢您对此的任何见解!提前感谢您的帮助!

您可以在这个数组上使用普通的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

只需array_reduce您的物品即可按订单价格分组:

$newArray = array_reduce($array, function($memo, $item){
  $memo[$item['order_id']] += $item['price'];
}, [])

编辑:请注意,您可以更好地在查询时GROUP/SUM您的数据。

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

可能:

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