<?php


/**
 * cartesian_product
 *
 * this method will calculate the cartesian product or any number of arrays
 *
 * @author    Bobby Whitman <bobby@dynamit.us>
 * @copyright Dynamit Technologies, LLC
 * @version   2010.04.05
 *
 * @param    array    any array (X_1)
 *        array    any array (X_2)
 *        ...
 *        array    any array (X_n)
 *
 * @return   array    array of ordered n-tuples that make up the cartesian product
 */
function cartesian_product() {
    
$opts func_get_args();

    
$n 1;
    
## need to get the size of our product
    
foreach( $opts as $field => $vals ) {
        
$index[$field] = 0;
        
$n $n count($vals);
    }

    
## build the combinations
    
$combo = array();
    for(
$i 0$i $n$i++ ) {

        
## grab the vars
        
$combo[$i] = array();
        
$incr true;
        foreach( 
$opts as $field => $vals ) {
            
$combo[$i][$field] = $vals[$index[$field]];

            if( 
$incr ) {
                
$index[$field] = ($index[$field]+1) % count($vals);
                
$incr = ($index[$field] == 0);
            }

        }

    }

    return 
$combo;
}


$sizes = array( 'XS''S''M''L''XL' );
$sleeves = array( 'Short sleeves''Long sleeves''Tank Top' );
$colors = array( 'red','blue''orange''green''white''black' );
$pockets = array( '0''1''2' );

$product cartesian_product$sizes$sleeves$colors$pockets );

echo 
'<pre>' print_r($product1) . '</pre>';

?>