1

我想立即道歉,因为我提出了一个没有太多先验信息的问题,但我不知道如何开始。

我需要根据已经存在的 SKU 的预设分组创建一个 SKU 列表,就像组合表或迭代表一样。

但是,手动执行此操作是一项荒谬的任务,我找不到任何 Javascript 站点可以让我根据需要执行此操作。

使用 PHP 类我认为这将是实现目标的更有效的方法。

目标:目标项目将由大约 5 种类型的项目组成。对于每种类型,它可以包含大约 15-20 个确切的代码。

例如 TYPE 1 可以是 Apple、Pear、Orange、Pineapple。等类型 2 可以是红色、绿色、蓝色、黑色。ETC

我需要能够为每个类型输入一个变体列表,并让它在可能存在的项目的每个可能迭代中连接变体。

我知道这会创建成千上万个(在这种情况下)SKU。

我遇到的问题是在创建迭代时,类型总是重复的。此外,这条规则也有例外,例如 Variant Apple 永远不会与 Variant Black 一起使用。所以这些类型永远不会在相同的串联代码中找到。

我敢肯定,对于某些人来说,这确实是非常简单的基本分组和类配置。因此,我非常感谢任何人可能为此提供的任何帮助。

非常感谢 - 刘易斯

4

1 回答 1

4

以下是非常快速和非常肮脏的东西,我只是把它放在一起为您提供帮助和一些开始......所以请不要“啊,那是垃圾代码注释”;)

<?php

class SkuGenerator2000
{
    public function generate($productId, array $variants, array $disallow)
    {
        // First lets get all of the different permutations = cartesian product
        $permutations = $this->permutate($variants);

        // Now lets get rid of the pesky combinations we don't want
        $filtered     = $this->squelch($permutations, $disallow);

        // Finally we can generate some SKU codes using the $productId as the prefix
        // this assumes you want to reuse this code for different products
        $skus         = $this->skuify($productId, $filtered);

        return $skus;
    }

    public function permutate(array $variants)
    {
        // filter out empty values
        // This is the cartesian product code
         $input  = array_filter($variants);
         $result = array(array());
         foreach ($input as $key => $values) {
             $append = array();
             foreach($result as $product) {
                 foreach($values as $item) {
                     $product[$key] = $item;
                     $append[] = $product;
                 }
             }
             $result = $append;
         }

         return $result;
    }

    public function squelch(array $permutations, array $rules)
    {
        // We need to loop over the differnt permutations we have generated
        foreach ($permutations as $per => $values) {
            $valid = true;
            $test  = array();
            // Using the values, we build up a comparison array to use against the rules
            foreach ($values as $id => $val) {
                // Add the KEY from the value to the test array, we're trying to make an
                // array which is the same as our rule
                $test[$id] = $val[0];
            }
            // Now lets check all of our rules against our new test array
            foreach ($rules as $rule) {
                // We use array_diff to get an array of differences, then count this array
                // if the count is zero, then there are no differences and our test matches
                // the rule exactly, which means our permutation is invalid
                if (count(array_diff($rule, $test)) <= 0) {
                    $valid = false;
                }
            }
            // If we found it was an invalid permutation, we need to remove it from our data
            if (!$valid) {
                unset($permutations[$per]);
            }
        }
        // return the permutations, with the bad combinations removed
        return $permutations;
    }

    public function skuify($productId, array $variants)
    {
        // Lets create a new array to store our codes
        $skus = array();

        // For each of the permutations we generated
        foreach ($variants as $variant) {
            $ids = array();
            // Get the ids (which are the first values) and add them to an array
            foreach ($variant as $vals) {
                $ids[] = $vals[0];
            }

            // Now we create our SKU code using the ids we got from our values. First lets use the
            // product id as our prefix, implode will join all the values in our array together using 
            // the separator argument givem `-`. This creates our new SKU key, and we store the original
            // variant as its value
            $skus[$productId . '-' . implode('-', $ids)] = $variant;
            // The bit above can be modified to generate the skues in a different way. It's a case of
            // dumping out our variant data and trying to figure what you want to do with it.
        }
        // finall we return our skus
        return $skus;
    }
}

// Possible variants
$variants = array(
    'brand' => array(
        // the first value in our array is our SKU identifier, this will be used to create our unqiue SKU code
        // the second value is a nice name, description if you will
        array('AP', 'Apple'),
        array('BA', 'Banana'),
        array('PE', 'Pear'),
    ),
    'color' => array(
        array('RE', 'Red'),
        array('GR', 'Green'),
        array('BL', 'Blue'),
    ),
);

// Rules for combinations I dont want
$disallow = array(
    array('brand' => 'AP', 'color' => 'GR'), // No green apples
    array('brand' => 'AP', 'color' => 'RE'), // No red apples
    array('brand' => 'PE', 'color' => 'BL'), // No blue pears
);

// Create new class
$skuGenerator = new SkuGenerator2000();

// Get me my skus!
$skus = $skuGenerator->generate('PHONE1', $variants, $disallow);

var_dump(array_keys($skus)); // Dump just the skus

// separate our data
echo "\n\n";

// Now dump the entire result!
var_dump($skus); // Dump it all
  • 生成 = 这需要您的指示
  • permutate = 从我之前提供的链接中窃取的笛卡尔积函数
  • 静噪=根据规则删除排列
  • skuify = 将产品 id 并生成 SKU 代码作为键,而变体仍然是值,因此它可以用于其他用途。
于 2016-09-28T14:26:35.237 回答