Printing an stdClass object

时光怂恿深爱的人放手 提交于 2020-01-06 03:40:30

问题


I have:

$value = $wpdb->get_row("SELECT custom_message FROM `wp_wpsc_cart_contents` WHERE purchaseid='" . $purchase_log['id'] . "'");

if I do:

print_r($value);

I get:

stdClass Object
(
    [custom_message] =>  |Castor Seed Oil  $4.45| 
)

So I tried to get that value doing:

foreach($value as $index => $result) {
   echo $result["custom_message"];
}

I also tried:

foreach($value as $index => $result) {
   echo $result->custom_message;
}

but that prints nothing, any idea what I'm doing wrong here?


回答1:


The loop does nothing, you are iterating an object with a single property that you already know the name of. Just do this:

echo $value->custom_message;



回答2:


No need for the for loop. Just do

  echo $value->custom_message;


来源:https://stackoverflow.com/questions/8497729/printing-an-stdclass-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!