How to merge 3 arrays, group, and count duplicates?

允我心安 提交于 2021-02-05 08:50:48

问题


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

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

My code:

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

I am getting this as output:

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

I want this output:

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 
        )

)

回答1:


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.

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.

Code: (Demo)

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

Output:

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


来源:https://stackoverflow.com/questions/60679989/how-to-merge-3-arrays-group-and-count-duplicates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!