I need to create an dynamic array and can't get it right. I need something like this:
Product Name
- top
- White
- Black
- Bottom
- Red
- Green
I came up with this code, which is producing the above text, so my logic must be about right.
$set = array();
$set['name'] = "Product Name";
$options = array("top", "bottom");
$values['top'] = array("White", "Black");
$values['bottom'] = array("Red", "Green");
echo "<pre>".$set['name']."</pre>";
foreach ($options as $o) {
echo "<pre>- $o</pre>";
$set['options'][]['name'] = $o;
foreach ($values[$o] as $v) {
echo "<pre>-- $v</pre>";
$set['options'][]['values']['name'] = $v;
}
}
The array that is created with the above code is:
Array
(
[name] => Product Name
[options] => Array
(
[0] => Array
(
[name] => top
)
[1] => Array
(
[values] => Array
(
[name] => White
)
)
[2] => Array
(
[values] => Array
(
[name] => Black
)
)
[3] => Array
(
[name] => bottom
)
[4] => Array
(
[values] => Array
(
[name] => Red
)
)
[5] => Array
(
[values] => Array
(
[name] => Green
)
)
)
)
THe output I want is:
Array
(
[name] => Product Name
[options] => Array
(
[0] => Array
(
[name] => top
[values] => Array
(
[0] => Array
(
[name] => White
)
[1] => Array
(
[name] => Black
)
)
)
[1] => Array
(
[name] => bottom
[values] => Array
(
[0] => Array
(
[name] => Red
)
[1] => Array
(
[name] => Green
)
)
)
)
)
What am I missing ?
array('top' => array('top','bottom'));