Miya Miya - 5 months ago 38
JSON Question

How to decode multiple json array in codeigniter

How to decode the below json array and insert into database

{`user`:[{"user_id":1,
"address_id":4,
"total_quantity" :8,
"total_amount":12000},],
"products":[{"products_name":"Shirt",
"quantity":4,
"price":1000},
{"products_name":"sari",
"quantity":4,
"price":2000}]}}

Answer

Try this

$data = json_decode($json);

foreach($data->users as $user) 
{
    $user_id = $user->user_id;
    $address _id = $user->address_id;
    //similar for other data and save it to database 
}

For products

foreach($data->products as $product) 
{
    $products_name = $product->products_name;
    $quantity = $product->quantity;
    //similar for other data and save it to database 
}
Comments