1

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.

8
  • 2
    if json was so '{"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'}, '[]')); Commented Feb 3, 2016 at 9:57
  • Yeah.. Removed the brackets using the following code $string_ids = str_replace(array('[',']'),array(' '),$product_ids);. But Only "3" is stored in database. Commented Feb 3, 2016 at 10:02
  • I rewrote my comment/ look now Commented Feb 3, 2016 at 10:03
  • the result is $product_ids = 31494847 Commented Feb 3, 2016 at 10:07
  • eval.in/512452 Commented Feb 3, 2016 at 10:08

4 Answers 4

1

While obtaining the JSON value, I suggest you drop the square brackets. If it's vital for you to have them in your JSON, remove them in your PHP code.

$product_ids = trim($product_ids, "[]");

Once removed, you can extract numbers in your string into an array:

$productIdArray = explode(",", $product_ids);

Now, in $productIdArray you will have your values:

Array (
    0 => "31",
    1 => "49",
    2 => "48",
    3 => "47"
)

And now you can iterate over this array in order to put the required values into your database.

foreach($productIdArray as $productId) {
    mysql_query("INSERT INTO table_name(column_name) VALUES ('$productId') ");
}

Also please reconsider using mysql_*, it's deprecated

Sign up to request clarification or add additional context in comments.

Comments

0

if json was so {"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

$json = '{"products":"[31, 49, 48, 47]"}';

$obj = json_decode($json);
$product_ids = explode(', ', trim($obj->{'products'}, '[]'));
for($x = 0; $x < sizeof($product_ids); $x++)
{
$row = $product_ids[$x];
$q = mysql_query("Insert into table_name(column_name) values ('$row') ");
}   

Demo

Comments

0

Try this

$obj = '{"products":"[31, 49, 48, 47]"}';
$objArray = json_decode($obj,true);
$objArray = explode(',',trim($objArray['products'], "[]"));
foreach ($objArray as $key => $value){

   $q = mysql_query("Insert into table_name(column_name) values ('trim($value)') ");
}

Comments

0

try like this

<?php

$json = '{"product":[31,49,48,47]}'; 
$obj = json_decode($json,true);
$product_ids= $obj['product'];
for($x = 0; $x < sizeof($product_ids); $x++)
{
$row = $product_ids[$x];
// $q = mysql_query("Insert into table_name(column_name) values ('$row') ");
}?>

work fine

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.