$ json = file_get_contents('php:// input');
// Decode the received JSON and store into $obj
$obj = json_decode($json,true);
foreach($obj as $product){
$product_id = $product['product_id'];
$data=array("product_id"=>$product_id);
DB::table('order_products')->insert($data);
}
答案 0 :(得分:1)
foreach循环中$product
里面是什么?
示例:
如果其值为array( product_id => 1 )
,则$product['product_id']
的结果为1
。
但是如果出现错误,并且$product
例如{"product_id":1}
(字符串而不是数组),则$product['product_id']
的结果将是错误。
可能的解决方法:
您可以在foreach循环内尝试$product = json_decode($product, true);
,也许$product
未被解码,仍然是字符串,因此可以解决您的问题。