我有一个包含许多输入的表单,但我在这里关心的两个被命名attributes[]
,options[$index][]
这是我attributes[]
输入中的示例数据的样子:
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">
这就是数据的options[$index][]
样子:
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">
(请注意,有时来自的索引值attributes[]
可能与对应数组 on 的索引值不匹配options[$index][]
)
我正在尝试动态创建一个表,其中每个非空元素都有一个列,每个attributes[]
可用选项的组合都有一个行,因此对于上面的表,这是一个示例表的样子:
+-------+-------+----------+-----+
| Size | Color | Material | SKU |
+-------+-------+----------+-----+
| Small | Red | Cotton | 100 |
| Small | Red | Wool | 101 |
| Small | Blue | Cotton | 102 |
| Small | Blue | Wool | 103 |
| Small | Green | Cotton | 104 |
| ... | ... | ... | ... |
我已经有一个计算数组的笛卡尔积的函数,它输出一个数组的组合(参考:https ://stackoverflow.com/a/12305169/1043817 )
我不确定如何从输入中获取所有值,将它们转换为数组并将它们输入CartProd()
函数。在这一点上,我主要关心的是option[$index][]
输入。
从上面的示例数据中,这就是我想要生成的:[small, medium, large] [Red, Blue, Green] [Cotton, Wool]