Jump to content

Ordered Permutations of a 2 dimensional array


NancyM

Recommended Posts

I read this thread and it almost solves my problem: http://www.phpfreaks.com/forums/index.php?topic=213809.0

 

but not quite.

 

I want to preserve an order to my permutations.  For instance my data set is like this:

 

$arr = array(0=>array("tee shirt","golf clubs","bread"), 1=>array("blue", "black", "brown"), 2=>array("large", "medium","small"));

 

I want to have all combinations of items, colors and sizes but they need to stay in the order of item - color - size.

 

Thanks for any insight!

Link to comment
Share on other sites

$arr = array(0=>array("tee shirt","golf clubs","bread"), 1=>array("blue", "black", "brown"), 2=>array("large", "medium","small"));

foreach($arr[0] as $item) {
   foreach($arr[1] as $colour) {
      foreach($arr[2] as $size) {
         echo $item.' '.$colour.' '.$size.'<br />';
      }
   }
}

 

Link to comment
Share on other sites

Alternately like this for an arbitrary amount of arrays:

 

function permutations(array $array)
{
switch (count($array)) {
	case 1:
		return $array[0];
		break;
	case 0:
		throw new InvalidArgumentException('Requires at least one array');
		break;
}

$a = array_shift($array);
$b = permutations($array);

$return = array();
foreach ($a as $v) {
	foreach ($b as $v2) {
		$return[] = array_merge(array($v), (array) $v2);
	}
}

return $return;
}

$arr = array(
array("tee shirt","golf clubs","bread"),
array("blue", "black", "brown"),
array("large", "medium","small"),
);

print_r(permutations($arr));

 

And in Haskell, just for fun:

[ (item, color, size) | item <- ["tee shirt","golf clubs","bread"], color <- ["blue", "black", "brown"], size <- ["large", "medium","small"] ]

or generally:

perm [] = [[]]
perm (x:xs) = [ x':xs' | x' <- x, xs' <- perm xs ]

Link to comment
Share on other sites

And in Haskell, just for fun:

[ (item, color, size) | item <- ["tee shirt","golf clubs","bread"], color <- ["blue", "black", "brown"], size <- ["large", "medium","small"] ]

or generally:

perm [] = [[]]
perm (x:xs) = [ x':xs' | x' <- x, xs' <- perm xs ]

 

Haha. You're liking Haskell then?

Link to comment
Share on other sites

  • 7 months later...

Daniel0: your example with permutation doesnt work correctly... could you please fix it because i have no idea how... for example:

 

Array

(

    [0] => Array

        (

            [0] =>

            [1] => pp

            [2] => 9

            [3] => 99

            [4] => o

            [5] => oo

            [6] => l

            [7] => ll

            [8] => 0

            [9] => 00

            [10] => p

            [12] => -

            [13] => --

        )

 

    [1] => Array

        (

            [0] =>

            [1] => aa

            [2] => q

            [3] => qq

            [4] => a

            [6] => z

            [7] => zz

            [8] => w

            [9] => ww

            [10] => s

            [11] => ss

            [12] => x

            [13] => xx

        )

 

    [2] => Array

        (

            [0] =>

            [1] => tt

            [2] => 4

            [3] => 44

            [4] => r

            [5] => rr

            [6] => f

            [7] => ff

            [8] => 5

            [9] => 55

            [10] => t

            [12] => g

            [13] => gg

            [14] => 6

            [15] => 66

            [16] => y

            [17] => yy

            [18] => h

            [19] => hh

        )

 

    [3] => Array

        (

            [0] =>

            [1] => rr

            [2] => 3

            [3] => 33

            [4] => e

            [5] => ee

            [6] => d

            [7] => dd

            [8] => 4

            [9] => 44

            [10] => r

            [12] => f

            [13] => ff

            [14] => 5

            [15] => 55

            [16] => t

            [17] => tt

            [18] => g

            [19] => gg

        )

 

    [4] => Array

        (

            [0] =>

            [1] => zz

            [2] => a

            [3] => aa

            [4] => z

            [6] => s

            [7] => ss

            [8] => x

            [9] => xx

        )

 

)

 

Your function for this array wont generate all permutations...

 

For example there will be no:

 

pattrz

 

or even

 

ppaattrrzz

 

Best regards,

FlashT

Link to comment
Share on other sites

My bad... i modified it to return array of words because i need to do somethng with them before returning, and doing it after takes too much ram... but was bad modification... could you please help me with that?

 

function permutations(array $array)
{
switch (count($array)) {
	case 1:
		return $array[0];
		break;
	case 0:
		throw new InvalidArgumentException('Requires at least one array');
		break;
}

$a = array_shift($array);
$b = permutations($array);

$return = array();
foreach ($a as $v) {
	foreach ($b as $v2) {
		$word = implode('',array_merge(array($v), (array) $v2));
		if(<something_with_word>)
			$return[] = implode('',array_merge(array($v), (array) $v2));
	}
}

return $return;
}

 

How do I make it work correctly?

Link to comment
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.