-1

i've array like this:-

[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
            )

    )

i want to store it in db like this:- Purple L 100 1 Purple S 200 2 and so on

It should store 12 records with diff combination of this array. I've tried but not able to succeed. Any help will be appreciated.

Shaeldon
  • 873
  • 4
  • 18
  • 28

1 Answers1

1

Not sure I got your question correctly but assuming each descriptor (color/size/price/qty) represents the same number of items you could just do something like:

$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";
}
user2910443
  • 111
  • 3
  • And do not be tempted to transform this into a simple INSERT-Statement, but pls. make sure to use prepared statements to prevent SQL-Injection! http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – MBaas Feb 23 '15 at 11:35