简体   繁体   English

如何解析此PHP stdClass对象?

[英]How do I parse this PHP stdClass Object?

The raw output of my object is when I do a print_r is: 我的对象的原始输出是当我执行print_r

Array ( [0] => stdClass Object ( [COUNT(*)] => 3 ) )

How do I get to the 3 ? 我如何到达3

This object is the result of a sql query inside wordpress with the $wpdb class. 该对象是在Wordpress中使用$wpdb类进行SQL查询的结果。

I am a noob at PHP objects. 我是PHP对象的菜鸟。 Also would like to know 也想知道

  • where do I learn to do object parsing stuff like this? 我在哪里学习做这样的对象解析东西?
  • What kind of object is this? 这是什么物体? Why is it wrapped in an Array? 为什么将它包装在数组中?

UPDATE: here's the source code: 更新:这是源代码:

global $wpdb;
$post_count = $wpdb->get_results("SELECT COUNT(*) FROM $wpdb->posts");
print_r($post_count);

It's not an object. 这不是一个对象。 It's an array containing an object. 这是一个包含对象的数组。 In that specific example, assuming that the variable is named $variable , you'd do this: 在该特定示例中,假设变量名为$variable ,则可以这样做:

echo $variable[0]->{'COUNT(*)'};

I found the solution: 我找到了解决方案:

global $wpdb;
$post_count = $wpdb->get_results("SELECT COUNT(*) as postcount FROM $wpdb->posts");
print_r($post_count[0]->postcount);

I needed to use an alias. 我需要使用别名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM