在单个数组php中合并四个多个数组并存储在db中

时间:2015-02-23 10:48:45

标签: php mysql

我是这样的阵列: -

[Attrdet] => Array
        (
            [Color] => Array
                (
                    [0] => Purple
                    [1] => Purple
                    [2] => Purple
                    [3] => Purple
                    [4] => Purple
                    [5] => Purple
                    [6] => Pink
                    [7] => Pink
                    [8] => Pink
                    [9] => Pink
                    [10] => Pink
                    [11] => Pink
                )
        [Size] => Array
            (
                [0] => L
                [1] => S
                [2] => M
                [3] => XL
                [4] => XXL
                [5] => XXXL
                [6] => L
                [7] => S
                [8] => M
                [9] => XL
                [10] => XXL
                [11] => XXXL
            )

        [price] => Array
            (
                [0] => 100
                [1] => 200
                [2] => 300
                [3] => 400
                [4] => 500
                [5] => 600
                [6] => 700
                [7] => 800
                [8] => 900
                [9] => 1000
                [10] => 1100
                [11] => 1200
            )

        [quantity] => Array
            (
                [0] => 1
                [1] => 2
                [2] => 3
                [3] => 4
                [4] => 5
                [5] => 6
                [6] => 1
                [7] => 7
                [8] => 5
                [9] => 3
                [10] => 5
                [11] => 7
            )

    )

我想将它存储在db中,如下所示: - 紫色L 100 1 紫色S 200 2等等

它应该使用此数组的diff组合存储12条记录。我已经尝试但未能成功。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

不确定我是否正确地提出了您的问题,但假设每个描述符(颜色/尺寸/价格/数量)代表相同数量的项目,您可以执行以下操作:

$length = count($array['Attrdet']['Color']); //grab from either if all have the same length

for ($i = 0; $i < $length; $i++) {
    echo "Insert" .
            " color " . $array['Attrdet']['Color'][$i] . 
            " size " . $array['Attrdet']['Size'][$i] . 
            " price " . $array['Attrdet']['price'][$i] . 
            " quantity " . $array['Attrdet']['quantity'][$i] . "\n";
}