尝试获取非对象的属性尽管它确实存在

eri*_*hak -1 php object

我知道这被问了很多,但我有这个代码:

    foreach($prices as $price){

$provider_id = $price->provider_id;
$prod_id = $price->product_id;
$name = $price->prod_name;
$desc = $price->desc;
$price = $price->price;
$original_price = $price->original_price;
$reward = $price->reward;
    }
Run Code Online (Sandbox Code Playgroud)

现在,如果print_r我得到:

    stdClass Object
(
    [id] => 1214
    [price_list_id] => 123
    [product_id] => 136
    [provider_id] => 1
    [price] => 0
    [reward] => 0
    [original_price] => 135
    [prod_name] => some text
    [desc] => some text with ' and " and ,
)
Run Code Online (Sandbox Code Playgroud)

知道是什么原因引起的吗?我得到错误original_pricereward

Tee*_*mas 5

这是错误,你在循环中2 variables命名$price,

 $price = $price->price;
Run Code Online (Sandbox Code Playgroud)

和,

 foreach($prices as $price){
Run Code Online (Sandbox Code Playgroud)