php - 创建包含所有可能变体的产品数组

标签 php laravel

我正在尝试生成一个包含产品所有变体的数组。可以有无限数量的属性和变体。

提供的数组:

["Shirt"]           # Product
    ["Color"]       # Attribute
        ["Green"]   # Variation
        ["Red"]
        ["Blue"]
    ["Size"]
        ["Small"]
        ["Medium"]
        ["Large"]

预期结果:

[0]
    ["type" => "Shirt"]
    ["color" => "Green"]
    ["Size" => "Small"]
[1]
    ["type" => "Shirt"]
    ["color" => "Green"]
    ["Size" => "Medium"]
...
[4]
    ["type" => "Shirt"]
    ["color" => "Red"]
    ["Size" => "Medium"]
[4]
    ["type" => "Shirt"]
    ["color" => "Red"]
    ["Size" => "Large"]

我尝试过 Laravel 和 PHP 的内置函数,但没有成功。我真的很感激任何见解。

最佳答案

你的问题似乎与我的通用方法很接近:

$arr = [
    'size'  => ['XS', 'S', 'M'],
    'color' => ['yellow', 'brown', 'white'],
    'weight'=> [
        'light' => [
            'super',
            'medium'
        ],
        'normal',
        'heavy' => [
            'regular',
            'most', 
            'overload'
        ]
    ]
];

function variations($array) {
    if (empty($array)) {
        return [];
    }

    function traverse($array, $parent_ind) {
        $r = [];
        $pr = '';

        if(!is_numeric($parent_ind)) {
            $pr = $parent_ind.'-';
        }

        foreach ($array as $ind => $el) {
            if (is_array($el)) {
                $r = array_merge($r, traverse($el, $pr.(is_numeric($ind) ? '' : $ind)));
            } elseif (is_numeric($ind)) {
                $r[] = $pr.$el;
            } else {
                $r[] = $pr.$ind.'-'.$el;
            }
        }

        return $r;
    }

    //1. Go through entire array and transform elements that are arrays into elements, collect keys
    $keys = [];
    $size = 1;

    foreach ($array as $key => $elems) {
        if (is_array($elems)) {
            $rr = [];

            foreach ($elems as $ind => $elem) {
                if (is_array($elem)) {
                    $rr = array_merge($rr, traverse($elem, $ind));
                } else {
                    $rr[] = $elem;
                }
            }

            $array[$key] = $rr;
            $size *= count($rr);
        }

        $keys[] = $key;
    }

    //2. Go through all new elems and make variations
    $rez = [];
    for ($i = 0; $i < $size; $i++) {
        $rez[$i] = [];

        foreach ($array as $key => $value) {
            $current = current($array[$key]);
            $rez[$i][$key] = $current;
        }

        foreach ($keys as $key) {
            if (!next($array[$key])) {
                reset($array[$key]);
            } else {
                break;
            }
        }
    }

    return $rez;
}

die(print_r(variations($arr)));

Result 将是 attributes 或其他内容的所有可能组合的数组(见下文)。我用它来创建测试 Woocommerce Variable 产品。

Testing started at 11:37 ...
/usr/bin/php /usr/local/bin/phpunit --bootstrap /opt/lampp/htdocs/unittests.lo/wp-content/plugins/test-woocommerce-izettle/tests/bootstrap.php --configuration /opt/lampp/htdocs/unittests.lo/wp-content/plugins/test-woocommerce-izettle/phpunit.xml.dist --teamcity
Array
(
    [0] => Array
        (
            [size] => XS
            [color] => yellow
            [weight] => light-super
        )

    [1] => Array
        (
            [size] => S
            [color] => yellow
            [weight] => light-super
        )

    [2] => Array
        (
            [size] => M
            [color] => yellow
            [weight] => light-super
        )

    [3] => Array
        (
            [size] => XS
            [color] => brown
            [weight] => light-super
        )

    [4] => Array
        (
            [size] => S
            [color] => brown
            [weight] => light-super
        )

    [5] => Array
        (
            [size] => M
            [color] => brown
            [weight] => light-super
        )

    [6] => Array
        (
            [size] => XS
            [color] => white
            [weight] => light-super
        )

    [7] => Array
        (
            [size] => S
            [color] => white
            [weight] => light-super
        )

    [8] => Array
        (
            [size] => M
            [color] => white
            [weight] => light-super
        )

    [9] => Array
        (
            [size] => XS
            [color] => yellow
            [weight] => light-medium
        )

    [10] => Array
        (
            [size] => S
            [color] => yellow
            [weight] => light-medium
        )

    [11] => Array
        (
            [size] => M
            [color] => yellow
            [weight] => light-medium
        )

    [12] => Array
        (
            [size] => XS
            [color] => brown
            [weight] => light-medium
        )

    [13] => Array
        (
            [size] => S
            [color] => brown
            [weight] => light-medium
        )

    [14] => Array
        (
            [size] => M
            [color] => brown
            [weight] => light-medium
        )

    [15] => Array
        (
            [size] => XS
            [color] => white
            [weight] => light-medium
        )

    [16] => Array
        (
            [size] => S
            [color] => white
            [weight] => light-medium
        )

    [17] => Array
        (
            [size] => M
            [color] => white
            [weight] => light-medium
        )

    [18] => Array
        (
            [size] => XS
            [color] => yellow
            [weight] => normal
        )

    [19] => Array
        (
            [size] => S
            [color] => yellow
            [weight] => normal
        )

    [20] => Array
        (
            [size] => M
            [color] => yellow
            [weight] => normal
        )

    [21] => Array
        (
            [size] => XS
            [color] => brown
            [weight] => normal
        )

    [22] => Array
        (
            [size] => S
            [color] => brown
            [weight] => normal
        )

    [23] => Array
        (
            [size] => M
            [color] => brown
            [weight] => normal
        )

    [24] => Array
        (
            [size] => XS
            [color] => white
            [weight] => normal
        )

    [25] => Array
        (
            [size] => S
            [color] => white
            [weight] => normal
        )

    [26] => Array
        (
            [size] => M
            [color] => white
            [weight] => normal
        )

    [27] => Array
        (
            [size] => XS
            [color] => yellow
            [weight] => heavy-regular
        )

    [28] => Array
        (
            [size] => S
            [color] => yellow
            [weight] => heavy-regular
        )

    [29] => Array
        (
            [size] => M
            [color] => yellow
            [weight] => heavy-regular
        )

    [30] => Array
        (
            [size] => XS
            [color] => brown
            [weight] => heavy-regular
        )

    [31] => Array
        (
            [size] => S
            [color] => brown
            [weight] => heavy-regular
        )

    [32] => Array
        (
            [size] => M
            [color] => brown
            [weight] => heavy-regular
        )

    [33] => Array
        (
            [size] => XS
            [color] => white
            [weight] => heavy-regular
        )

    [34] => Array
        (
            [size] => S
            [color] => white
            [weight] => heavy-regular
        )

    [35] => Array
        (
            [size] => M
            [color] => white
            [weight] => heavy-regular
        )

    [36] => Array
        (
            [size] => XS
            [color] => yellow
            [weight] => heavy-most
        )

    [37] => Array
        (
            [size] => S
            [color] => yellow
            [weight] => heavy-most
        )

    [38] => Array
        (
            [size] => M
            [color] => yellow
            [weight] => heavy-most
        )

    [39] => Array
        (
            [size] => XS
            [color] => brown
            [weight] => heavy-most
        )

    [40] => Array
        (
            [size] => S
            [color] => brown
            [weight] => heavy-most
        )

    [41] => Array
        (
            [size] => M
            [color] => brown
            [weight] => heavy-most
        )

    [42] => Array
        (
            [size] => XS
            [color] => white
            [weight] => heavy-most
        )

    [43] => Array
        (
            [size] => S
            [color] => white
            [weight] => heavy-most
        )

    [44] => Array
        (
            [size] => M
            [color] => white
            [weight] => heavy-most
        )

    [45] => Array
        (
            [size] => XS
            [color] => yellow
            [weight] => heavy-overload
        )

    [46] => Array
        (
            [size] => S
            [color] => yellow
            [weight] => heavy-overload
        )

    [47] => Array
        (
            [size] => M
            [color] => yellow
            [weight] => heavy-overload
        )

    [48] => Array
        (
            [size] => XS
            [color] => brown
            [weight] => heavy-overload
        )

    [49] => Array
        (
            [size] => S
            [color] => brown
            [weight] => heavy-overload
        )

    [50] => Array
        (
            [size] => M
            [color] => brown
            [weight] => heavy-overload
        )

    [51] => Array
        (
            [size] => XS
            [color] => white
            [weight] => heavy-overload
        )

    [52] => Array
        (
            [size] => S
            [color] => white
            [weight] => heavy-overload
        )

    [53] => Array
        (
            [size] => M
            [color] => white
            [weight] => heavy-overload
        )

)

Process finished with exit code 0

关于php - 创建包含所有可能变体的产品数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52501402/

相关文章:

php - mysql INSERT INTO循环不插入一些行

php - MySQL 的简单 PHP 表单出错

javascript - SQLSTATE[01000] : Warning: 1265 Data truncated for column 'id_paket' at row 1

php - LARAVEL - 显示存储文件夹中的图像

php - 为什么 Laravel 的 App::getLocale() 方法不一致?

php - jquery只返回第一行的值

php - 无法识别我在 PHP 中进行的查询中的 MySQL 错误

php - Laravelcollective/html 在 Laravel 5.5 中不工作

laravel - Laravel 中发送多个附件邮件时出现问题

javascript - 根据另一列计算列 laravel