Jump to content

getting combinations of array index or elements


blazing_bruce

Recommended Posts

Hello all,
i have an array named $checked;

$checked[0] = 1;
$checked[1] = 3;
$checked[2] = 4;
$checked[3] = 6;
$checked[4] = 7;


i want to make combinations like
array index
0 1 2 3 means $checked[0]; $checked[1]; $checked[2]; $checked[3];
0 1 2 4
0 1 3 4
0 2 3 4
1 2 3 4
. . . .

like this manner. it should find all the possible combinations.

here i said the sizeof array is five. but it may be 7 , 8 or 3 . if the size of array is 'n' then i want combinations in size of 'n-1' .

is it possible? please help me.

Thank you,

Link to comment
Share on other sites

i writtened an algorithm for it myself :)

$checked[0] = 1;
$checked[1] = 3;
$checked[2] = 4;
$checked[3] = 6;
$checked[4] = 7;

if you want to get all posiblities of array elements (with out repeating)
like this
0 1 2 4
0 1 2 3
0 1 3 4
0 2 3 4
1 2 3 4

PHP code follows
[code]for($n=0;$n<=$total_checked;$n++)
        {
        unset($new);
        $j=0;
        //echo("N=$n<br>");
        $value = $total_checked-$n;
        //echo("value = $value<br>");
            for($b=0;$b<$total_checked;$b++)
            {
            if($b==$value)
            {
        //    echo("Entered condition");
        $notaccepted = $checked[$b];
            continue;
            }
            $new[$j]=$checked[$b];
            echo($j);
//Change here if you want that combination of array values echo("$new[$j]");
            $j++;
            }
            if($n==0)
            {
            continue;
            }
        //    print_r($new);
            echo("<BR>");
            
        }    [/code]

Thank you,
Karthi keyan
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.