I have list of 100 First Name and 100 Surname. I want use mix of all the combinations without repetition. Do you have some math solutions for this?
Asked
Active
Viewed 712 times
0
-
Have you tried anything yourself? – Epodax Jan 28 '15 at 09:35
-
You don't need permutations but combinations. Look here: http://en.wikipedia.org/wiki/Cartesian_product – Piotr Olaszewski Jan 28 '15 at 09:38
1 Answers
3
this is called "cartesian product", php man page on arrays http://php.net/manual/en/ref.array.php shows some implementations (in comments).
and here's yet another one:
function array_cartesian() {
$_ = func_get_args();
if(count($_) == 0)
return array(array());
$a = array_shift($_);
$c = call_user_func_array(__FUNCTION__, $_);
$r = array();
foreach($a as $v)
foreach($c as $p)
$r[] = array_merge(array($v), $p);
return $r;
}
$cross = array_cartesian(
array('A', 'B', 'C'),
array('1', '2')
);
print_r($cross);

Pixel
- 1,946
- 2
- 15
- 26