Page 1 of 1

need help with probable recursive situation

Posted: Fri Feb 07, 2003 8:26 pm
by BarefootRoss
I've got a situation that's driving me buggy. Maybe someone can give me a hand.

Given a multidimensional array like this:

$product_options = array( array("red","blue","green"),
array("small","medium","large"),
array("longsleeve","shortsleeve")
);

I need to end up with a an array of strings like this (consider each line to be a separate element in the array):

red:small:longsleeve
red:medium:longsleeve
red:large:longsleeve
red:small:shortsleeve
red:medium:shortsleeve
red:large:shortsleeve
blue:small:longsleeve

and so on, covering all potential combinations of the items in the array.

The $product_options array can contain any number of elements in any combinations.

I tend to think I would need to handle this with nested for loops and/or recursive functions, but I can't get my arms around this conceptually. Can anyone give me a nudge in the right direction?

Thanks in advance for any help.

Ross

Posted: Fri Feb 07, 2003 11:05 pm
by gyardleydn
Heres some basic logic. Obviously I did no debugging, just the basic idea is here. Hopefully you can take this and finish it

Code: Select all

option_types_index = number of product_options arrays - 1

if max_options_index < 0 
	ERROR: no arrays

max_options_index = 1

for x = 0 to option_types_index
	&#123;
	max_index&#1111;x] = number of elements in sub array
	if max_index&#1111;x] == 0
		ERROR: empty sub array
	max_options_index *= number of elements
	&#125;

max_options_index = max_options_index - 1

for x= 0 to max_options_index
	&#123;
	current_index = x MODULUS max_index&#1111;0]
	master_index = (x - current_index) / max_index&#1111;0]
	build_list = product_options&#1111;0]&#1111;current_index]  

	for  y = 1 to  option_types_index
		&#123;
		current_index = master_index MODULUS max_index&#1111;y]
		master_index = (master_index - current_index) / max_index&#1111;y]
		build_list = ":" + product_options&#1111;y]&#1111;current_index]  + build_list
		&#125;
	option_list&#1111;] = build_list
	&#125;
editted
build_list = ":" + product_options[y][current_index] + build_list
should be
build_list = build_list + ":" + product_options[y][current_index]

Posted: Sat Feb 08, 2003 4:01 pm
by BarefootRoss
Many thanks for the assist. After a bit of debugging, I came up with the following, which works great. I don't think I would have thought of using the modulus operator no matter how long I stared at it. I very much appreciate you taking some time with it.

Ross

Code: Select all

<?php
$product_options = array( 	array("red","blue","green"),
							array("small","medium"),
							array("long","short") 
							);


$option_types_index = count($product_options);

if ($option_types_index < 0) {
	return "error";
}

$max_options_index = 1;

for ($x=0; $x < $option_types_index; $x++) {
	$max_index[$x] = count($product_options[$x]);
	$max_options_index *= $max_index[$x];
}

for ($x=0; $x < $max_options_index; $x++) {
	$current_index = $x % $max_index[0];
	$master_index = ($x - $current_index) / $max_index[0];
	$build_list = $product_options[0][$current_index];
	
	for ($y=1; $y<$option_types_index; $y++) {
		$current_index = $master_index % $max_index[$y];
		$master_index = ($master_index - $current_index) / $max_index[$y];
		$build_list .= ":" . $product_options[$y][$current_index];
	}
	
	$option_list[] = $build_list;

}

?>