使用PHP创建SKU代码

使用PHP创建SKU代码,php,class,sku,Php,Class,Sku,我想马上道歉,因为我问了一个事先信息很少的问题,但我甚至不知道如何开始 我需要根据已经存在的预设SKU分组创建SKU列表,如组合表或迭代表 然而,手动执行这项任务是一项荒谬的任务,我找不到任何Javascript站点允许我按要求执行 使用PHP类,我认为这将是实现这一目标的更有效的方法 目标:目标项目将由5种左右的项目组成。对于每种类型,大约有15-20个确切的代码可以组成 例如,类型1可以是苹果、梨、橘子、菠萝。等 类型2可以是红色、绿色、蓝色、黑色。等 我需要能够为每种类型输入一个变量列表,

我想马上道歉,因为我问了一个事先信息很少的问题,但我甚至不知道如何开始

我需要根据已经存在的预设SKU分组创建SKU列表,如组合表或迭代表

然而,手动执行这项任务是一项荒谬的任务,我找不到任何Javascript站点允许我按要求执行

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

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

例如,类型1可以是苹果、梨、橘子、菠萝。等 类型2可以是红色、绿色、蓝色、黑色。等

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

我知道这将创建10个数千个(在本例中)SKU

我遇到的问题是,在创建迭代时,类型总是重复的。此外,这条规则也有例外,例如,变体苹果永远不可能与变体黑一起使用。因此,在相同的级联代码中永远找不到这些类型

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


非常感谢-刘易斯

以下是一些非常快速和非常肮脏的东西,我刚刚把它放在一起给你一个帮助和一些开始。。。所以请不要“啊,这是垃圾代码注释”;)


以下是一些非常快速和肮脏的东西,我刚刚把它放在一起给你一个帮助和一些开始。。。所以请不要“啊,这是垃圾代码注释”;)



很遗憾,我在工作电脑前,这里没有我的任何代码。因此,我向您表示衷心的歉意。听起来您需要找到一组包含不同变体类型的数组的笛卡尔乘积。这将产生所有不同的排列。。。然后,您需要过滤掉带有不应匹配的变体的不良结果。您可以通过创建一系列规则并处理所有生成的变量来实现这一点。最后是一些将这些转换为SKU的逻辑。。。也许为每种类型分配唯一的代码?笛卡尔积!以前从没听说过,但在看了数学之后,这听起来很像我需要的。但不知道如何在PHP中实现这一点。幸好您使用了Stackoverflow!我很高兴我能哭。这给了我一个很好的出发点!我仍然不知道,但至少现在我知道这个原理叫什么,我可以学会如何制作一个。如果有人对如何为这一原则创建例外裁决有任何建议,我将非常感激,我可以也将自己做这件事,但遗憾的是,我在这件事上与时间不符,因此欢迎任何想要帮助的人访问。遗憾的是,我在我的工作计算机旁,这里没有我的任何代码。因此,我向您表示衷心的歉意。听起来您需要找到一组包含不同变体类型的数组的笛卡尔乘积。这将产生所有不同的排列。。。然后,您需要过滤掉带有不应匹配的变体的不良结果。您可以通过创建一系列规则并处理所有生成的变量来实现这一点。最后是一些将这些转换为SKU的逻辑。。。也许为每种类型分配唯一的代码?笛卡尔积!以前从没听说过,但在看了数学之后,这听起来很像我需要的。但不知道如何在PHP中实现这一点。幸好您使用了Stackoverflow!我很高兴我能哭。这给了我一个很好的出发点!我仍然不知道,但至少现在我知道这个原理叫什么,我可以学会如何制作一个。如果有人对如何为这一原则创建例外裁决有任何建议,我将非常感激,我可以也将自己做这件事,但遗憾的是,我在这件事上与时间不符,所以欢迎任何想要帮助的人加入。哇,太快了,太神奇了,我真的非常感激。这给了我一个更好的观点,关于例外规则,这正是我所需要的。尽管您在这里使用了一些我不熟悉的做法,但您是否可以向我解释一下这是如何工作的?我宁愿学习,也不愿让你为我做。但我知道这是一个很高的要求。这会在转储中生成字符串位置标识符,是否有方法删除这些标识符并将它们按行拆分?如果您删除最后一行的
数组键($SKU)
,只需执行
变量转储($SKU)
,您就会看到它包含所有内容。我会在代码中添加一些注释,这样可以帮助你成为明星!再次非常感谢,我将详细介绍这一切,并了解它的工作原理,这样我就可以进行必要的修改,以产生我所需的准确结果。您确实为我节省了一个月的工作时间,因此我无法对您表示足够的感谢。因此,目前,这会产生一个数据输出,其中提供了一些不太有用的字符串和数字引用。目前看起来是这样的:-[0]=>string(18)“SKU-SKU-SKU”[1]=>string(20)“SKU-SKU-SKU”有没有办法让输出排除字符串引用并在每一行上分开?哇,这很快,太神奇了,我真的非常感谢。这给了我一个更好的观点,关于例外规则,这正是我所需要的。尽管您在这里使用了一些我不熟悉的做法,但您是否可以向我解释一下这是如何工作的?我宁愿学习,也不愿让你为我做。但我知道这是一个很高的要求。这会在转储中生成字符串位置标识符,是否有方法删除这些标识符并将它们按行拆分?如果删除
array\k
<?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