# getting combinations of array index or elements

2 replies to this topic

### #1 blazing_bruce

blazing_bruce
• Members
• Member
• 28 posts
• LocationIndia

Posted 26 March 2006 - 09:32 PM

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,

### #2 blazing_bruce

blazing_bruce
• Members
• Member
• 28 posts
• LocationIndia

Posted 27 March 2006 - 12:02 AM

### #3 blazing_bruce

blazing_bruce
• Members
• Member
• 28 posts
• LocationIndia

Posted 27 March 2006 - 07:21 PM

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
```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>");

}    ```

Thank you,
Karthi keyan

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users