douye4254 2013-02-25 18:15
浏览 13
已采纳

在PHP中显示Std类的值[关闭]

All, I have the following code to get some posts from Tumblr:

$baseHostname = "name.tumblr.com";
$tumblrConsumerKey = "asfd"; # use your own consumer key here
$humblr = new Humblr($baseHostname, $tumblrConsumerKey);

$post = $humblr->getPosts(array('limit' => 1));
print_r($post);

This works fine and gives me a result of something like this:

Array ( 
    [0] => stdClass Object ( 
        [blog_name] => name 
        [id] => 43993 
        [post_url] => http://name.tumblr.com/post/43993/
        [slug] => slug 
        [type] => video 
        [date] => 2013-02-25 18:00:25 GMT 
        [timestamp] => 1361815225 
        [state] => published 
        [format] => html )

I try and display some values like this:

echo "The blog name is: ".$post->blog_name;
echo $post->id;

However, it is blank. How can I display these values?

Thanks

  • 写回答

2条回答 默认 最新

  • dtz30833 2013-02-25 18:56
    关注

    For a start, turn on error reporting.

    // error reporting for development environment
    error_reporting(-1);
    ini_set('display_startup_errors', 1);
    ini_set('display_errors', 1);
    

    As @Zlatan points out, it's an array of stdClass's.

    After you enable error reporting, you will error notices "Notice: Trying to get property of non-object in ..." for this code:

    echo "The blog name is: ".$post->blog_name;
    echo $post->id;
    

    Because you're trying to access an non-object.

    You can fix it by accessing the object through it's array index:

    echo "The blog name is: ".$post[0]->blog_name;
    echo $post[0]->id;
    

    Assuming $posts

    Array
    (
        [0] => stdClass Object
            (
                [blog_name] => blog1
                [id] => 10234
                [post_url] => http://name.tumblr.com/post/43993/
                [slug] => slug
                [type] => video1
                [date] => 2013-02-25 18:00:25 GMT
                [timestamp] => 1361815225
                [state] => published
                [format] => html
            )
    
        [1] => stdClass Object
            (
                [blog_name] => blog2
                [id] => 20234
                [post_url] => http://name.tumblr.com/post/43993/
                [slug] => slug1
                [type] => video
                [date] => 2013-02-25 18:00:25 GMT
                [timestamp] => 1361815225
                [state] => published
                [format] => html
            )
    
    )
    

    Accessing objects by array index:

    echo "The blog name is: ".$post[0]->blog_name;
    echo $post[0]->id;
    echo "The blog name is: ".$post[1]->blog_name;
    echo $post[1]->id;
    
    // prints
    // The blog name is: blog1
    // 10234
    // The blog name is: blog2
    // 20234
    

    If you want to loop the posts:

    foreach ($posts as $post) {
        echo "The blog name is: ".$post->blog_name;
        echo $post->id;
    }
    
    // prints
    // The blog name is: blog1
    // 10234
    // The blog name is: blog2
    // 20234
    

    Resources

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题