需要合并数组与匹配值

时间:2018-01-02 14:22:51

标签: php arrays

我有一个像这样的数组我将如何得到新数组

Array
(
    [0] => Array
        (
            [model] => MX555
            [color] => RED
            [qty] => 2
        )

    [1] => Array
        (
            [model] => MX555
            [color] => RED
            [qty] => 2
        )

    [2] => Array
        (
            [model] => MX555
            [color] => Green
            [qty] => 2
        )

    [3] => Array
        (
            [model] => MX555
            [color] => Green
            [qty] => 1
        )
)

我需要像这样返回数组

Array
(
    [0] => Array
        (
            [model] => MX555
            [color] => RED
            [qty] => 4
        )



    [1] => Array
        (
            [model] => MX555
            [color] => Green
            [qty] => 4
        )
)

2 个答案:

答案 0 :(得分:1)

我假设你想要通过多个值识别重复的项目,在这种情况下modelcolor(如果你只想按单个值color进行分组,那么请参阅相反,RamC的答案)。一种方法是创建一个数组,其中每个键都是通过连接这些值来创建的:

function mergeQuantitiesByModelAndColor(array $array)
{
    $merged_array = array();
    foreach ($array as $item) {
        $key = $item['model'] . '|' . $item['color'];
        if (!isset($merged_array[$key])) {
            $merged_array[$key] = $item;
        } else {
            $merged_array[$key]['qty'] += $item['qty'];
        }
    }
    return array_values($merged_array);
}

如下测试(php 5.6):

$array = array(
    array(
        'model' => 'MX555',
        'color' => 'RED',
        'qty' => 2,
    ),
    array(
        'model' => 'MX555',
        'color' => 'RED',
        'qty' => 2,
    ),
    array(
        'model' => 'MX555',
        'color' => 'Green',
        'qty' => 2,
    ),
    array(
        'model' => 'MX555',
        'color' => 'Green',
        'qty' => 1,
    ),
);

var_dump(mergeQuantitiesByModelAndColor($array));

提供输出:

array(2) {
  [0]=>
  array(3) {
    ["model"]=>
    string(5) "MX555"
    ["color"]=>
    string(3) "RED"
    ["qty"]=>
    int(4)
  }
  [1]=>
  array(3) {
    ["model"]=>
    string(5) "MX555"
    ["color"]=>
    string(5) "Green"
    ["qty"]=>
    int(3)
  }
}

答案 1 :(得分:0)

Sum amount按元素分组

根据您的问题,您想要添加类似物品的数量是不可能的。

如果是这种情况,那么您可以使用以下代码段来实现它。

在下面的代码段中,我使用元素color对项目进行了分组。

根据您的要求随意更改。

<强>段

$group_by = 'color';

foreach ($data as $item) {
    if (!isset($res[$item[$group_by]])) {
        $res[$item[$group_by]] =  $item;
    } else {
        $res[$item[$group_by]]['qty'] += $item['qty'];
    }
}

<强>代码

$data = [
  [
      'model' => "MX555",
      'color' => "RED",
      'qty' => 2,
  ],
  [
      'model' => "MX555",
      'color' => "RED",
      'qty' => 2,
  ],
  [
      'model' => "MX555",
      'color' => "Green",
      'qty' => 2,
  ],
  [
      'model' => "MX555",
      'color' => "Green",
      'qty' => 1,
  ],
];

$res = [];
$group_by = 'color';

foreach ($data as $item) {
    if (!isset($res[$item[$group_by]])) {
        $res[$item[$group_by]] =  $item;
    } else {
        $res[$item[$group_by]]['qty'] += $item['qty'];
    }
}

print_r(array_values($res));

<强>输出

Array
(
    [0] => Array
        (
            [model] => MX555
            [color] => RED
            [qty] => 4
        )

    [1] => Array
        (
            [model] => MX555
            [color] => Green
            [qty] => 3
        )

)