dataController.php第98行中的ErrorException:未定义属性:stdClass :: $ id Laravel

时间:2017-01-02 08:12:13

标签: php json laravel

我正在尝试从JSON请求中获取“id”值。但我收到此错误未定义的属性:stdClass :: $ id Laravel。

$data = json_decode($request->getContent());
foreach ($data->data as $data){
            $id = $data->id;


  $modsyPrice = floor($data->modsyPrice);
}

这是我的Json:

{"data":{"0":{"ID":"1","Name":"3 Piece Roundabout Candle Holders","Url": 

"http://url","ModsyStatus":"0","weRplayStatus"

:"0", "ModsyPrice" :"39.95", "weRplayPrice":"39.95","activeLoader":true} }}

2 个答案:

答案 0 :(得分:1)

带有大写字母的

ID$id = $data->ID;

ModsyPrice

相同

答案 1 :(得分:0)

尝试如下:您的数组是多维的。

/*$json = '{"data":{"0":{"ID":"1","Name":"3 Piece Roundabout Candle Holders","Url": 

"http://url","ModsyStatus":"0","weRplayStatus"

:"0", "ModsyPrice" :"39.95", "weRplayPrice":"39.95","activeLoader":true} }}';*/


$json =json_decode($request->getContent(),true); //get your json
$array = json_decode($json,true);
//print_r($array);
    foreach ($array['data'] as $data){
                $id = $data['ID'];
                $modsyPrice = floor($data['ModsyPrice']);
    }
echo $id."<br/>";
echo $modsyPrice;