2016-06-22 78 views
0

解碼多種JSON數組如何以下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

試試這個

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

謝謝! .. itz工作得很好:) – Miya

0

喜歡的東西

$data = json_decode($data); 

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