2016-09-28 1 views
1

Ich wollte mich auf Anhieb dafür entschuldigen, dass ich eine Frage mit sehr wenig Vorinformation gestellt habe, aber ich bin nicht in der Lage, überhaupt anzufangen.Erstellen von SKU-Codes mit PHP

Ich muss eine Liste von SKUs basierend auf einer voreingestellten Gruppierung von SKUs erstellen, die bereits vorhanden ist, wie eine Kombinationstabelle oder Iterationstabelle.

Allerdings ist dies eine lächerliche Aufgabe, und ich konnte keine Javascript-Seite finden, die es mir erlauben würde, es nach Bedarf zu tun.

Mit PHP-Klassen dachte ich, es wäre ein viel effizienter Weg, um das Ziel zu erreichen.

ZIEL: Das Zielelement besteht aus 5 oder so Arten von Elementen. Für jeden TYPE gibt es ungefähr 15-20 exakte Codes, aus denen er bestehen könnte.

Zum Beispiel könnte Typ 1 Apfel, Birne, Orange, Ananas sein. Usw. TYP 2 könnte Rot, Grün, Blau, Schwarz sein. Etc

Ich muss in der Lage sein, eine Liste von Varianten für jeden TYPE einzugeben und es die Varianten in jeder möglichen Iteration eines Elements, das vorhanden sein könnte, verketten.

Ich bin mir bewusst, dass dies 10 Tausende von (in diesem Fall) SKUs erstellen würde.

Das Problem, auf das ich stoße, ist beim Erstellen der Iterationen die Typen immer duplizieren. Außerdem gibt es Ausnahmen von dieser Regel, zum Beispiel könnte Variant Apple niemals mit Variant Black sein. Diese Typen würden also niemals in demselben verketteten Code gefunden werden.

Ich bin sicher, dass für jemanden aus ihrem dies ist wirklich sehr einfach grundlegende Gruppierung und Klassenkonfiguration. Deshalb schätze ich jede Hilfe sehr, die jemand dafür haben könnte.

Vielen Dank - Lewis

+0

Leider bin ich an meinem Arbeitscomputer und habe keinen Code von mir hier. Daher die herzliche Entschuldigung. – LewisMCT333

+1

Klingt so, als müssten Sie das kartesische Produkt einer Gruppe von Arrays finden, die die verschiedenen Variantentypen enthalten. Dies würde alle verschiedenen Permutationen erzeugen ... Sie müssen dann die schlechten Ergebnisse mit Varianten ausfiltern, die nicht übereinstimmen sollten. Sie können dies tun, indem Sie ein Array von Regeln erstellen und alle generierten Varianten verarbeiten. Endlich eine Logik, um diese in SKUs zu verwandeln ... vielleicht jedem Code einen eindeutigen Code zuzuordnen? –

+0

Kartesische Produkte! Ich habe noch nie davon gehört, aber nachdem ich mir die Mathematik angeschaut habe, die sehr ähnlich klingt wie ich es brauche. Keine Ahnung wie man das in PHP macht. – LewisMCT333

Antwort

3

Hier finden Sie etwas sehr schnell und sehr schmutzig, habe ich es nur zusammen geworfen Sie eine helfende Hand und etwas zu geben, mit zu beginnen ... also bitte nicht „ah thats Müll Code Kommentare“;)

<?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 
  • erzeugen = dies führt Ihre Anweisungen
  • permutate = die kartesische Produkt-Funktion aus dem Link gestohlen ich vor
  • vorgesehen
  • squelch = entfernt Permutationen basierend auf den Regeln
  • skuify = nimmt die Produkt-ID und generiert SKU-Codes als Schlüssel und die Variante bleibt der Wert, so dass sie für etwas anderes verwendet werden kann.
+0

Wow, das war schnell, das ist erstaunlich, ich bin wirklich sehr dankbar. Dies gibt mir einen viel besseren Ausgangspunkt in Bezug auf die Ausnahmeregeln und das ist genau das, was ich brauche. Obwohl du hier einige Praktiken benutzt hast, die mir nicht vertraut sind, könntest du mir auf die Gefahr hin, viel zu viel von dir zu fragen, erklären, wie das funktioniert? Ich würde lieber lernen, als nur Sie es für mich tun zu lassen. Aber ich bin mir bewusst, dass dies eine große Anfrage ist. – LewisMCT333

+0

Dies erzeugt String Location Identifier im Dump. Gibt es trotzdem welche, um diese zu entfernen und sie nach Zeilen zu teilen? – LewisMCT333

+0

Wenn Sie 'array_keys ($ skus)' in der letzten Zeile entfernen und einfach 'var_dump ($ skus)' verwenden, sehen Sie, dass es alles enthält. Ich werde dem Code einige Kommentare hinzufügen, damit er hilft –