循环属性以创建库存变化

时间:2013-12-30 07:47:16

标签: php arrays inventory variations

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

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. 让我们假设我在数组中有另外两个attrs:

    [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
        )
    
  3. 如何循环使用此数组来创建如下变体:

    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
    

    提前致谢!

2 个答案:

答案 0 :(得分:1)

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

一个。是的,我创建了一个包含所有属性的关联数组:

阵 (

[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;
}    

℃。然后我将它添加到这个结构中的表中:

See tables stru

答案 1 :(得分:0)

可能先从“清理”您的数据开始:

$attr = array();

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

所以你有类似

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

从那以后这应该是有帮助的:
How to generate in PHP all combinations of items in multiple arrays