循环属性以创建库存变化 [英] loop on attributes to create inventory variations
问题描述
例如,我具有以下属性数组:
I have this array of attributes for example:
Array
(
[0] => Array
(
[id] => 20
[title] => Brown
[parent_id] => 1
[parent_title] => Color
[isMultiple] => 1
)
[1] => Array
(
[id] => 21
[title] => Cream
[parent_id] => 1
[parent_title] => Color
[isMultiple] => 1
)
[2] => Array
(
[id] => 61
[title] => S
[parent_id] => 2
[parent_title] => Size
[isMultiple] => 1
)
[3] => Array
(
[id] => 62
[title] => M
[parent_id] => 2
[parent_title] => Size
[isMultiple] => 1
)
[4] => Array
(
[id] => 63
[title] => L
[parent_id] => 2
[parent_title] => Size
[isMultiple] => 1
)
)
从这个数组中我们可以了解到,我们有6种不同的广告资源:
from this array we can understand that we have 6 variations of inventory:
1 | Brown | S
2 | Brown | M
3 | Brown | L
4 | Cream | S
5 | Cream | M
6 | Cream | L
-
像上面的示例一样,循环遍历此数组以创建6个变体的另一个数组的正确方法是什么.
What is the correct way to loop over this array to create another array of the 6 variations like in the above example.
让我们假设我在数组中还有另外2个attrs,如下所示:
let's assume that I have another 2 attrs in the array like this:
[5] => Array
(
[id] => 64
[title] => Cotton
[parent_id] => 3
[parent_title] => Metiral
[isMultiple] => 1
)
[6] => Array
(
[id] => 65
[title] => Wool
[parent_id] => 3
[parent_title] => Metiral
[isMultiple] => 1
)
如何在此数组上循环创建类似这样的变体:
How I can loop on this array to create variations like these:
1 | Brown | S | wool
2 | Brown | S | cotton
3 | Brown | M | wool
4 | Brown | M | cotton
5 | Brown | L | wool
6 | Brown | L | cotton
7 | Cream | S | wool
8 | Cream | S | cotton
9 | Cream | M | wool
10 | Cream| M | cotton
11 | Cream| L | wool
12 | Cream| L | cotton
提前谢谢!
推荐答案
好,我找到了解决方法:
OK I found the solution:
A.正确,我对所有属性都做了一个关联数组:
A. right, I made an associative arrays of all my attributes:
数组 (
[0] => Array
(
[0] => 20
[1] => 21
)
[1] => Array
(
[0] => 61
[1] => 62
[2] => 63
)
)
B.然后我递归地创建整个可能的变体:
B. then I recursively create the whole possible variations:
function buildVariants($arrays, $i = 0) {
if (!isset($arrays[$i])) {
return array();
}
if ($i == count($arrays) - 1) {
return $arrays[$i];
}
// get combinations from subsequent arrays
$tmp = $this->buildVariants($arrays, $i + 1);
$result = array();
// concat each array from tmp with each element from $arrays[$i]
foreach ($arrays[$i] as $v) {
foreach ($tmp as $t) {
$result[] = is_array($t) ? array_merge(array($v), $t) : array($v, $t);
}
}
return $result;
}
C.然后将其添加到这种结构的表中:
C. then I add it to tables in this structure:
这篇关于循环属性以创建库存变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!