如何合并3个数组,分组和计数重复项? [英] How to merge 3 arrays, group, and count duplicates?

查看:54
本文介绍了如何合并3个数组,分组和计数重复项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个长度相等的相关数组,其中包含产品详细信息.

I have three related arrays of equal length containing product details.

我正在尝试合并它们,对列数据进行分组(基于id,大小和颜色),并保持每个独特产品的运行计数.

I am trying merge them, group the columnar data (based on id, size, and color), and keep a running count of each unique product.

我的代码:

$a1 = array(1,1,1,2,5,1);
$a2 = array("m","m","m","s","xl","s");
$a3 = array("color","color","color3","color1","color2","color1",);

$temp = [];
foreach($a1 as $index => $product_id) {
    $size = $a2[$index];
    $colors = $a3[$index];
    // if an entry for given product id and size combination already exists, then the current
    // counter value is incremented by 1; otherwise it gets initialized with 1
    $temp[$product_id][$size] = $temp[$product_id][$size] == $temp[$product_id][$color] ? $temp[$product_id][$size]+1 : 1;
    $temp[$product_id][$colors] = $temp[$product_id][$colors]  ? $temp[$product_id][$colors]+1 : 1;
}
$result_temp = [];
foreach($temp as $product_id => $size_data) {
    foreach($size_data as $size => $count) {
        $result_temp[] = [$product_id, $size, $count];
    }
}
echo "<pre>";
print_r($result_temp);

我得到这个作为输出:

[1] => Array
    (
        [0] => 1
        [1] => color
        [2] => 2
    )

[2] => Array
    (
        [0] => 1
        [1] => color3
        [2] => 1
    )

[3] => Array
    (
        [0] => 1
        [1] => s
        [2] => 1
    )

[4] => Array
    (
        [0] => 1
        [1] => color1
        [2] => 1
    )

[5] => Array
    (
        [0] => 2
        [1] => s
        [2] => 1
    )

[6] => Array
    (
        [0] => 2
        [1] => color1
        [2] => 1
    )

[7] => Array
    (
        [0] => 5
        [1] => xl
        [2] => 1
    )

[8] => Array
    (
        [0] => 5
        [1] => color2
        [2] => 1
    )
)

我想要这个输出:

Array
(
    [0] => Array
        (
            [0] => 1  product id
            [1] => m color
            [2] => 2 this is count of m
            [3] => color this is color of product id in array 1 
        )

    [1] => Array
        (
            [0] => 1 product id 
            [1] => m color
            [2] => 1 this is count of  m
            [3] => color3 this is color of product id in array 1 
        )

    [2] => Array
        (
            [0] => 1 product id
            [1] => s color
            [2] => 1 this is count of s
            [3] => color1 this is color of product id in array 1 
        )

    [3] => Array
        (
            [0] => 2 product id
            [1] => s color
            [2] => 1 this is count of xl
            [3] => color1 
        )

    [4] => Array
        (
            [0] => 5 product id
            [1] => xl color
            [2] => 1  this is count of xl
            [3] => color2 
        )

)

推荐答案

我建议减小分组数组的深度.最简单的方法是使用复合临时密钥(基于ID,大小和颜色)进行分组.

I recommend reducing the depth of your grouping array. Most simply, use a composite temporary key (based on id and size and color) to do the grouping.

迭代之后,如果要删除临时键,请调用 array_values().

After iterating, call array_values() if you want to remove the temporary keys.

我对您的输入数组和输出值采取了一些自由,以使您的问题和解决方案更具表现力.

I took some liberties with your input array and your output values to help to make your issue and my solution a little more expressive.

代码:(演示)

$ids = [1, 1, 1, 2, 5, 1];
$sizes = ["m", "m", "m", "s", "xl", "s"];
$colors = ["red", "red", "yellow", "orange", "red", "orange"];

foreach ($ids as $i => $id) {
    $size = $sizes[$i];
    $color = $colors[$i];
    $tempKey = "{$id}:{$size}:{$color}";
    if (!isset($result[$tempKey])) {
        $result[$tempKey] = [
            'id' => $id,
            'size' => $size,
            'color' => $color,
            'stock' => 1
        ];
    } else {
        ++$result[$tempKey]['stock'];
    }
}
var_export(array_values($result));

输出:

array (
  0 => 
  array (
    'id' => 1,
    'size' => 'm',
    'color' => 'red',
    'stock' => 2,
  ),
  1 => 
  array (
    'id' => 1,
    'size' => 'm',
    'color' => 'yellow',
    'stock' => 1,
  ),
  2 => 
  array (
    'id' => 2,
    'size' => 's',
    'color' => 'orange',
    'stock' => 1,
  ),
  3 => 
  array (
    'id' => 5,
    'size' => 'xl',
    'color' => 'red',
    'stock' => 1,
  ),
  4 => 
  array (
    'id' => 1,
    'size' => 's',
    'color' => 'orange',
    'stock' => 1,
  ),
)

这篇关于如何合并3个数组,分组和计数重复项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆