I have a form with many inputs but the two I am concerned with here are named attributes[]
and options[$index][]
This is what sample data in my attributes[]
input looks like:
Array
(
[0] => Size
[1] => Color
[2] => Material
)
Note: The above is the $_POST data visualized as PHP array
Below is what the HTML form would look like:
<input name="attributes[]" value="Size">
<input name="attributes[]" value="Color">
<input name="attributes[]" value="Material">
And this is what data from options[$index][]
looks like:
Array
(
[1] => Array
(
[0] => Small
[1] => Medium
[2] => Large
[3] =>
)
[2] => Array
(
[0] => Red
[1] => Blue
[2] => Green
[3] =>
)
[3] => Array
(
[0] => Cotton
[1] => Wool
[2] =>
)
)
Note: The above is the $_POST data visualized as PHP array
Below is what the HTML form would look like:
<input name="options[1][]" value="Small">
<input name="options[1][]" value="Medium">
<input name="options[1][]" value="Large">
<input name="options[2][]" value="Red">
<input name="options[2][]" value="Blue">
<input name="options[2][]" value="Green">
<input name="options[3][]" value="Cotton">
<input name="options[3][]" value="Wool">
(note sometimes the index-value from attributes[]
may not match the index-value on corresponding array on options[$index][]
)
I'm trying to create a table dynamically that has one column for every non-empty element in attributes[]
and a row for every COMBINATION of options available so for the table above this is what a sample table would look like:
+-------+-------+----------+-----+
| Size | Color | Material | SKU |
+-------+-------+----------+-----+
| Small | Red | Cotton | 100 |
| Small | Red | Wool | 101 |
| Small | Blue | Cotton | 102 |
| Small | Blue | Wool | 103 |
| Small | Green | Cotton | 104 |
| ... | ... | ... | ... |
I already have a function that computes the cartesian products of arrays which outputs an array of arrays with the combination (ref: https://stackoverflow.com/a/12305169/1043817)
I'm not really sure how I can get all the values from the inputs turn them into an array and feed them into the CartProd()
function. My main concern at this point are the option[$index][]
inputs.
From the sample data above, this this what I want to produce: [small, medium, large] [Red, Blue, Green] [Cotton, Wool]