Jump to content

Array Permutation


jeeva

Recommended Posts

try

<?php
$a = array(1,2,3,4);
function my_permut($a){
if (count($a) == 1)return array($a, array());
$out = array();
foreach ($a as $k => $v){
	unset($a[$k]);
	$b = my_permut($a);
	$out = array_merge(my_merge($v, $b), $out);
	$out = array_merge($out, $b);
	break;
}
return $out;
}
function my_merge($a, $b){
if (count($b) == 0) return array($a);
foreach ($b as $k => $v){
	array_unshift($v, $a);
	$b[$k] = $v;
}
return $b;
}
$b = my_permut($a);
print_r($b);
?>

Link to comment
https://forums.phpfreaks.com/topic/103840-array-permutation/#findComment-531660
Share on other sites

What are you trying to do? List all possible combinations?

 

Of course this is going to take a long time. There are n^n possibilities. The number of possibilities is going to grow pretty rapidly as n increases.

 

However sasa' code does not return all possible values. For example, proding two numbers, 1 and 2, it would not return 2,1 as a combination. The code provided appears to run relatively quickly for 12 numbers here.

Link to comment
https://forums.phpfreaks.com/topic/103840-array-permutation/#findComment-533287
Share on other sites

thanks GingerRobot

 

sasa code is  right for me. that is wt i expected actually ..

here 1,2 is equal to 2,1

 

By using this i want to search candidate profile..

for example,

 

if user will give keyword like a,b,c

i want to search all possibilities like

1. a and b and c

2. a and b

3. b and c

4. a and c

and a,b,c

 

 

Thanks

Jeeva

Link to comment
https://forums.phpfreaks.com/topic/103840-array-permutation/#findComment-533289
Share on other sites

Archived

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

×
×
  • 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.