Combinations using PHP Arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
codersrini
Forum Newbie
Posts: 3
Joined: Thu May 10, 2007 11:51 pm

Combinations using PHP Arrays

Post 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]
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post 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));
codersrini
Forum Newbie
Posts: 3
Joined: Thu May 10, 2007 11:51 pm

Post 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]
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

codersrini wrote:friends, Thx for all ur replies.
You're welcome, although you apparently didn't bother to read them. ;)
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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.
codersrini
Forum Newbie
Posts: 3
Joined: Thu May 10, 2007 11:51 pm

Post 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
Post Reply