如何解码下面的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}]}}
答案 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
}