0

我有这个属性数组,例如:

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 种库存变化:

1 | Brown | S
2 | Brown | M
3 | Brown | L
4 | Cream | S
5 | Cream | M
6 | Cream | L
  1. 循环这个数组以创建另一个包含 6 个变体的数组的正确方法是什么,就像上面的例子一样。

  2. 假设我在数组中有另外 2 个属性,如下所示:

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

我如何循环这个数组来创建像这样的变体:

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

提前致谢!

4

2 回答 2

1

好的,我找到了解决方案:

A. 对,我创建了一个包含所有属性的关联数组:

大批 (

[0] => Array
    (
        [0] => 20
        [1] => 21
    )

[1] => Array
    (
        [0] => 61
        [1] => 62
        [2] => 63
    )

)

B. 然后我递归地创建所有可能的变化:

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. 然后我将它添加到这个结构中的表中:

见表结构

于 2013-12-30T15:07:02.770 回答
0

Maybe start with "cleaning" your data first:

$attr = array();

foreach( $attributes as $attribute ){
  $attr[$attribute['parent_title']][] = $attribute['title'];
} 

So that you have something like

array(
  "Color" => array(
    "Color1",
    "Color2",
    "Color3"
  ),
  "Size" => array(
    "Size1",
    "Size2",
    "Size3"
  ),
  //...
);

From there on this should be helpful:
How to generate in PHP all combinations of items in multiple arrays

于 2013-12-30T09:04:25.233 回答