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
need help with probable recursive situation
Moderator: General Moderators
-
BarefootRoss
- Forum Newbie
- Posts: 2
- Joined: Fri Feb 07, 2003 8:26 pm
- Location: Western Colorado
- gyardleydn
- Forum Commoner
- Posts: 27
- Joined: Tue Dec 03, 2002 8:27 am
Heres some basic logic. Obviously I did no debugging, just the basic idea is here. Hopefully you can take this and finish it
editted
build_list = ":" + product_options[y][current_index] + build_list
should be
build_list = build_list + ":" + product_options[y][current_index]
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
{
max_indexїx] = number of elements in sub array
if max_indexїx] == 0
ERROR: empty sub array
max_options_index *= number of elements
}
max_options_index = max_options_index - 1
for x= 0 to max_options_index
{
current_index = x MODULUS max_indexї0]
master_index = (x - current_index) / max_indexї0]
build_list = product_optionsї0]їcurrent_index]
for y = 1 to option_types_index
{
current_index = master_index MODULUS max_indexїy]
master_index = (master_index - current_index) / max_indexїy]
build_list = ":" + product_optionsїy]їcurrent_index] + build_list
}
option_listї] = build_list
}build_list = ":" + product_options[y][current_index] + build_list
should be
build_list = build_list + ":" + product_options[y][current_index]
-
BarefootRoss
- Forum Newbie
- Posts: 2
- Joined: Fri Feb 07, 2003 8:26 pm
- Location: Western Colorado
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
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;
}
?>