#### Archived

This topic is now archived and is closed to further replies.

# getting combinations of array index or elements

## 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' .

Thank you,

##### 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