如何合并3个数组,分组和计数重复项?

编程入门 行业动态 更新时间:2024-10-27 12:29:12
本文介绍了如何合并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个数组,分组和计数重复项?

本文发布于:2023-11-22 08:40:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616726.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!