Clemen Swichmann
Clemen Swichmann

Reputation: 83

storing arrays as value in mysql

I have a table called product attribute in which I want to store products attribute name and its value one by one. For example of user selected price as attribute name and 100,200,300 as its values then I want to store price with 100, 200 and 300 with product id. I've array like this:-

[attribute_name] => Array
        (
            [0] => Color
            [1] => weight
            [2] => Brand
            [3] => Price
        )

[Attrvalue] => Array
    (
        [0] => Black
        [1] => 2kg
        [2] => 3kg
        [3] => 4kg
        [4] => Sony
        [5] => Samsung
        [6] => LG
        [7] => 500
        [8] => 1000
        [9] => 2000
        [10] => 9000
        [11] => 9500
        [12] => 10000
    )

How to devide this array in key value pair like for color=>Black in php?

Upvotes: 0

Views: 95

Answers (3)

Rick James
Rick James

Reputation: 142218

If you have a data structure that is handy in PHP, but mysql does not need to see the details, use json_encode and json_decode. Then put the result in a TEXT column.

Upvotes: 0

Rao Biz
Rao Biz

Reputation: 9

foreach($data as $key=>$value)

{ $keys = "'".implode("',' ", $temp_tablev)."'";

$values = "'".implode("',' ", $temp_tablev)."'";

}

$sql = mysqli_query("INSERT INTO ($keys) VLAUES ($values)") or die(mysqli_error());

Upvotes: 0

Abhineet Kashyap
Abhineet Kashyap

Reputation: 180

You can name your form inputs like this attr[attrname] eg for single value attr for mutiple value attr use

<select name="attr['price'][]" multiple>
<option>...</option>
</select>

This will provide you an array like

array('attr'
           =>array('color'=>value),
           =>array(
                  'price'=>array(
                                 0=>value1,
                                 1=>value2
                                 )
                  )
);                                    

Upvotes: 1

Related Questions