blazing_bruce Posted March 26, 2006 Share Posted March 26, 2006 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 index0 1 2 3 means $checked[0]; $checked[1]; $checked[2]; $checked[3];0 1 2 40 1 3 4 0 2 3 41 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 https://forums.phpfreaks.com/topic/5882-getting-combinations-of-array-index-or-elements/ Share on other sites More sharing options...
blazing_bruce Posted March 27, 2006 Author Share Posted March 27, 2006 if you know the outline of an algorithm, then you are welcome. please help me. Link to comment https://forums.phpfreaks.com/topic/5882-getting-combinations-of-array-index-or-elements/#findComment-21020 Share on other sites More sharing options...
blazing_bruce Posted March 27, 2006 Author Share Posted March 27, 2006 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 40 1 2 30 1 3 40 2 3 41 2 3 4PHP 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 https://forums.phpfreaks.com/topic/5882-getting-combinations-of-array-index-or-elements/#findComment-21297 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.