【问题标题】:"message": "Illegal string offset 'product_id'", "exception": "ErrorException","message": "非法字符串偏移 'product_id'", "exception": "ErrorException",
【发布时间】:2020-10-10 18:00:12
【问题描述】:

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

【问题讨论】:

    标签: php json associative


    【解决方案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 没有被解码并且仍然是一个字符串,所以这可以解决你的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-03
      • 1970-01-01
      • 2020-10-21
      • 1970-01-01
      • 2013-09-30
      • 2020-11-29
      • 2017-09-13
      相关资源
      最近更新 更多