Generate all possibilities from multiple arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
virtualme
Forum Newbie
Posts: 3
Joined: Tue Nov 29, 2011 10:53 am

Generate all possibilities from multiple arrays

Post by virtualme »

Hello,

We are trying to make a small shop with product variants. So, for instance, a red, medium sized shirt. The SKU (or product code) will be a combination of all the information. So, the SKU for a shirt might be "ABC", we then want to append the ID's of the "red" and "medium" to the SKU to generate a complete collection of possible options so we can then assign stock to them (Hope I made this clear).

I'm a bit stuck as to how I can generate all these unique SKU's nicely without having duplicates (e.g. we only want "ABC-Red-Medium" without "ABC-Medium-Red"). I have this code at the moment, but am a bit lost - someone please help me!

Thank you!

Code: Select all

<?php
// All possible values
$start = array(
    'colour' => array(1  => '-red',   2  => '-blue',   3 => '-pink'),
    'size'   => array(20 => '-small', 21 => '-medium', 22 => 'large'),
    'arrow'  => array(30 => '-up',    31 => '-right',  32 => '-down')
);

// All the combinations
$end = array();

// Loop over the first one
foreach ($start[0] as $id => $value) {
    // Start to loop over the other variants
    for ($i = 1; $i < count($start); $i++) {
        // Loop over each of the variants
        foreach ($start[$i] as $id => $value) {
            $end[] = $skuNew . $value;
        }
    }
}

// Print
echo '<pre>';
var_dump($end);
echo '</pre>';
virtualme
Forum Newbie
Posts: 3
Joined: Tue Nov 29, 2011 10:53 am

Re: Generate all possibilities from multiple arrays

Post by virtualme »

Oh, I forgot to say that we would like to use the ID's and not the value themselves, so we would actually want ABC-1-21-30 for a red shirt in size medium with an "up" arrow.

Thank you very much!
maxx99
Forum Contributor
Posts: 142
Joined: Mon Nov 21, 2011 3:40 am

Re: Generate all possibilities from multiple arrays

Post by maxx99 »

Example I found on http://www.theserverpages.com/php/manua ... .array.php
If you need keys just use http://theserverpages.com/php/manual/en ... y-keys.php on input arrays :) it should do the trick

Code: Select all

<?php
function array_cartesian_product($arrays) {
    
    //returned array...
    $cartesic = array();
    
    //calculate expected size of cartesian array...
    $size=(sizeof($arrays)>0)?1:0;
    foreach($arrays as $array)
    {
        $size= $size*sizeof($array);
    }
    for($i=0; $i<$size;$i++) {
        $cartesic[$i] = array();
        
        for($j=0;$j<sizeof($arrays);$j++)
        {
            $current = current($arrays[$j]); 
            array_push($cartesic[$i], $current);    
        }
        //set cursor on next element in the arrays, beginning with the last array
        for($j=(sizeof($arrays)-1);$j>=0;$j--)
        {
            //if next returns true, then break
            if(next($arrays[$j])) {
                break;
            } else {    //if next returns false, then reset and go on with previuos array...
                reset($arrays[$j]);
            }
        }
    }
    return $cartesic;
 }
//example
$arrays[0] = array("a", "b");
$arrays[1] = array("x", "y", "z");
print_r(array_cartesian_product($arrays));
?>

 The output is:
 Array
 (
    [0] => Array
        (
            [0] => a
            [1] => x
        )

    [1] => Array
        (
            [0] => a
            [1] => y
        )

    [2] => Array
        (
            [0] => a
            [1] => z
        )

    [3] => Array
        (
            [0] => b
            [1] => x
        )

    [4] => Array
        (
            [0] => b
            [1] => y
        )

    [5] => Array
        (
            [0] => b
            [1] => z
        )
 )
virtualme
Forum Newbie
Posts: 3
Joined: Tue Nov 29, 2011 10:53 am

Re: Generate all possibilities from multiple arrays

Post by virtualme »

You, Sir, are a gentleman and a scholar, and have just saved me hours of work. Thank you very much indeed.
Post Reply