Working with multidimensional array

Hello

I found this great function to calculate permutations

function array_combination($le, $set){

    $lk = combination_number($le, count($set));
    $ret = array_fill(0, $lk, array_fill(0, $le, '') );

    $temp = array();
    for ($i = 0 ; $i < $le ; $i++)
        $temp[$i] = $i;

    $ret[0] = $temp;

    for ($i = 1 ; $i < $lk ; $i++){
        if ($temp[$le-1] != count($set)-1){
            $temp[$le-1]++;
        } else {
            $od = -1;
            for ($j = $le-2 ; $j >= 0 ; $j--)
                if ($temp[$j]+1 != $temp[$j+1]){
                    $od = $j;
                    break;
                }
            if ($od == -1)
                break;
            $temp[$od]++;
            for ($j = $od+1 ; $j < $le ; $j++)    
                $temp[$j] = $temp[$od]+$j-$od;
        }
        $ret[$i] = $temp;
    }
    for ($i = 0 ; $i < $lk ; $i++)
        for ($j = 0 ; $j < $le ; $j++)
            $ret[$i][$j] = $set[$ret[$i][$j]];   

    return $ret;
}


$mySet = array("1","2","3","4");
print_r(array_combination(3, $mySet));

The result is shown in a multidimensional array .

Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 ) [1] => Array ( [0] => 1 [1] => 2 [2] => 4 ) [2] => Array ( [0] => 1 [1] => 3 [2] => 4 ) [3] => Array ( [0] => 2 [1] => 3 [2] => 4 ) )

What I have to do to show data in this way ?

1 2 3
1 2 4
1 3 4
2 3 4

I am using this

foreach ($mySet as $key =>$val) {
echo “
$val”;
}

but it returns only this

1
2
3
4

any help please ?

Thank you

since you have a two.dimensional array, you need two levels of looping. that could be two foreach() or an implode()/vprintf()/array_walk() inside a foreach().

could you show me how please , I am really lost with multidimensional array.

Incidentally, you may want to read the other thread you made about this. Cause…that’s… a very complicated way to generate permutations.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.