Below is my JSON data, where the value has array of data.
{"products":"[31, 49, 48, 47]"}
I need to store different products in different rows of my sql database.
Below is the PHP code where I able to get only the value.
$obj = json_decode($json);
$product_ids = $obj->{'products'};
The result of $product_ids is [31, 49, 48, 47]
Now I have to traverse through the array of values in $product_idsand store each value in different rows of MYSQL database.
To store the values in different rows of MYSQL I tried the below code.
for($x = 0; $x < sizeof($product_ids); $x++)
{
$row = $product_ids[$x];
$q = mysql_query("Insert into table_name(column_name) values ('$row') ");
}
But I am not able to achieve what I actually want. Above code stores only the square bracket([) inside MYSQL, I am not able to store the elements of the array.
I am very new to PHP. Any help would be really greatfull.
Thanks.
'{"products":[31, 49, 48, 47]}; your code will be fine. Now $product_ids is string[31, 49, 48, 47]. You should remove brackets from the string an use explode() function. For example so$product_ids = explode(', ', trim($obj->{'products'}, '[]'));$product_ids = 31494847