如何在codeigniter中解码多个json数组

时间:2016-06-22 05:42:59

标签: json codeigniter

如何解码下面的json数组并插入数据库

{`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}]}}

2 个答案:

答案 0 :(得分:0)

这样的东西
$data = json_decode($data);

foreach($data->products as $row) // if $data->products does not work try $data['products']
{
    $this->db->insert('products',$row)
}

答案 1 :(得分:0)

试试这个

$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 
}

对于产品

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