Page 1 of 1

Combinations using PHP Arrays

Posted: Thu May 10, 2007 11:55 pm
by codersrini
Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

I need a quick and simple solution for my query. I have a set of array, each index has another array with a set of elements. I need to generate the common possible combinations ouptput like given below.

Pls Help. Thx in Advance
Srini

Code: Select all

<?
//given array
$x[0] = array(1,3,5);
$x[1] = array(6,7);
$x[3] = array(8,10,12);

//desired ouptput
$z[] = "1,6,8";
$z[] = "1,6,10";
$z[] = "1,6,12";
$z[] = "1,7,8";
$z[] = "1,7,10";
$z[] = "1,7,12";

$z[] = "3,6,8";
$z[] = "3,6,10";
$z[] = "3,6,12";
$z[] = "3,7,8";
$z[] = "3,7,10";
$z[] = "3,7,12";

$z[] = "5,6,8";
$z[] = "5,6,10";
$z[] = "5,6,12";
$z[] = "5,7,8";
$z[] = "5,7,10";
$z[] = "5,7,12";
?>

Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri May 11, 2007 2:36 am
by Kieran Huggins
sounds to me like you want to nest some foreach loops.. this will be a good introduction to nested procedural thinking!

Post what you try and we'll help point you in the right direction.

Posted: Fri May 11, 2007 4:16 am
by stereofrog
What you're looking for is called "cartesian product" (the same thing SQL server does when you use JOINs). This simple recursive procedure generates CP for the given array of arrays:

Code: Select all

function product($a) {
	if(count($a) == 0)
		return array(array());
	foreach($a[0] as $e)
		foreach(product(array_slice($a, 1)) as $p)
			$r[] = array_merge(array($e), $p);
	return $r;
}

# example

$x[] = array(1,3,5);
$x[] = array(6,7);
$x[] = array(8,10,12);

print_r(product($x));

Posted: Sun May 13, 2007 4:44 am
by codersrini
Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


friends, Thx for all ur replies.

I forgot to mention an important thing,

the x array would keep on increasing, and also, each index of the x array would have dynamic arrays. Need bit recursive logic.

The algorithm, for this is based on sudoku game logic.

chkout the solution.

Code: Select all

$knt = count($x);

$result = getAllPermutations($x,"",$tmpArr="",$knt);

function getAllPermutations($mdArray, $firstTime=true, $tempArray=array(),$len) 
{
global $permutationResults;

$thisArray = array_shift($mdArray);

if(!empty($thisArray))
{
foreach($thisArray as $key => $elem) 
{
$tempArray[] = $elem;

getAllPermutations($mdArray, false, $tempArray,$len);

if($len == count($tempArray))
{
foreach($tempArray as $key => $val)
{
$ans .= "$val,";
}
$ans = rtrim($ans,",");
$permutationResults[] = $ans;
$ans = "";
}
array_pop($tempArray);
}
}
return $permutationResults;
}
OK!!!

Srini


Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sun May 13, 2007 6:18 am
by stereofrog
codersrini wrote:friends, Thx for all ur replies.
You're welcome, although you apparently didn't bother to read them. ;)

Posted: Sun May 13, 2007 10:39 pm
by Kieran Huggins
I think if you search there was a thread about sudoku solvers a few months ago - I wrote one about half way and never finished, but I seem to recall many other people having finished (and posted?) their results.

Posted: Tue May 15, 2007 7:52 am
by codersrini
stereofrog wrote:
codersrini wrote:friends, Thx for all ur replies.
You're welcome, although you apparently didn't bother to read them. ;)
Im really very sorry, for not noticing your reply. your code is too simple.

Srini