Jump to content

Recommended Posts

Hi friends can someone tall me how to make code who will calculate all combinations from one array.

 

example : array(2, 5, 7, 3, 4, 6)

 

examle: 2, 5, 7

2, 5, 6

2, 5, 3

2, 5, 7, 3

...

if I i want 3/6 should be 20 combinations

if I want 4/6 should be 10 combinations

or 5/6  6 combin.

 

can someone give idea how to make this

 

Thanks !!!

 

Link to comment
https://forums.phpfreaks.com/topic/127115-combinations/
Share on other sites

I don't quite understand what you want.

 

Count the number of permutations?

it's just n factorial (n! where n is the number of elements)

 

Find all possible permutations? Find a certain number of permutations based upon the number of elements? Based upon certain elements?

That's a bit more complicated, and no point giving you code for one question when you may have another.

 

Link to comment
https://forums.phpfreaks.com/topic/127115-combinations/#findComment-657713
Share on other sites

Ok i will give one more examlple:

array(green, fly, blue, dry, small);

from this 5 word I want all combinatitons from by 3 word

we have 3 from 5 that is 10 combinations

1. green, fly, blue

2. green, fly, dry

3. green, fly, small

4. green, blue, dry

5. green, dry, small

6. green, blue, small

7. fly, blue, dry

8. fly, blue, small

9. blue, dry, small

10. fly, dry, small

 

but i can`t make this in code. please if someone know to post it.

 

 

Link to comment
https://forums.phpfreaks.com/topic/127115-combinations/#findComment-658049
Share on other sites

try

<?php
function my_permut($a, $num, $start = 0){
if(!is_array($a)) return false;
if (count($a) == $num) return array($a);
if ($num == 1){
	$out = array();
	for ($i = $start; $i < count($a); $i++) $out[]=array($a[$i]);
	return $out;
}
$a = array_values($a);
$out = array();
for ($i = $start; $i <= count($a) - $num; $i++){
	$d = my_permut($a, $num-1, $i+1);
	foreach ($d as $e){
		array_unshift($e,$a[$i]);
		$out[] = $e;
	}
}
return $out;
}

$x = my_permut(array(1,2,3,4,5),3);
print_r($x);
?>

Link to comment
https://forums.phpfreaks.com/topic/127115-combinations/#findComment-658125
Share on other sites

OK i will try this code but i dont think that will be ok  because I want combinations not premutations

in combinations  1. green, fly, blue

                      2. fly, green, blue

are same but not in permitation

once again please help me if you have code for combinations.

Thanks !!!

 

Link to comment
https://forums.phpfreaks.com/topic/127115-combinations/#findComment-659277
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.