Php 如何合并3个阵列、分组和计数重复项?
我有三个长度相等的相关数组,其中包含产品详细信息 我正在尝试合并它们,对列数据进行分组(基于id、大小和颜色),并对每个独特的产品进行计数 我的代码:Php 如何合并3个阵列、分组和计数重复项?,php,arrays,grouping,Php,Arrays,Grouping,我有三个长度相等的相关数组,其中包含产品详细信息 我正在尝试合并它们,对列数据进行分组(基于id、大小和颜色),并对每个独特的产品进行计数 我的代码: $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 => $
$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);
我想要这个输出:
$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));
我建议减少分组数组的深度。最简单的方法是使用复合临时键(基于id、大小和颜色)进行分组 迭代后,如果要删除临时键,请调用
array\u values()
我对您的输入数组和输出值进行了一些调整,以使您的问题和我的解决方案更具表现力
代码:()
输出:
我建议减少分组数组的深度。最简单的方法是使用复合临时键(基于id、大小和颜色)进行分组 迭代后,如果要删除临时键,请调用
array\u values()
我对您的输入数组和输出值进行了一些调整,以使您的问题和我的解决方案更具表现力
代码:()
输出:
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
)
)
$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,
),
)