Make Probability With PHP

Wahyu Raja Butar-Butar Reply 06:08

I want simply to find-out better way to do this:
$array = array(
    array('a', 'b', 'c'),
    array('e', 'f', 'g'),
    array('h', 'i', 'j', 'k', 'l')
);
The goals is to print something like this:
a e h
a e i
a e j
a e k
a e l

a f h
a f i
a f j
a f k
a f l

a g h
a g i
a g j
a g k
a g l
    class Test {
    // holds the combinations


     var $combinations= array(); 



    function getCombinations($batch, $elements, $i)  {
        if ($i >= count($elements)) {
            $this->combinations[] = $batch;
        } else {     
            foreach ($elements[$i] as $element) {
                $this->getCombinations(array_merge($batch, $element), $elements, $i + 1);
            }                       
        }
    }
}  
// Test it!
$traits = array (
    array(
        array('Happy'),
        array('Sad'),
        array('Angry'),
        array('Hopeful')
    ),
    array(
        array('Outgoing'),
        array('Introverted')
    ),
    array(
        array('Tall'),
        array('Short'),
        array('Medium')
    ),
    array(
        array('Violent'),
        array('Quiet'),
        array('Psychotic')
    ),
    array(
        array('Handsome'),
        array('Plain'),
        array('Ugly')
    )
);

$test = new Test();
$start = array();
$test->getCombinations($start, $traits, 0);   
print_r($test->combinations);   
Source : http://stackoverflow.com/questions/17959127/php-array-join-each-sub-array-together-probability

Related Posts

Tutorial 1265733524951790677
Comments
0 Comments
Facebook Comments by Media Blogger

Post a Comment

Search

Google+ Followers

Popular Posts

Translate