访问数组中的对象 [英] Accessing Object within Array

查看:134
本文介绍了访问数组中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下输出:

Array (
  [0] => stdClass Object (
        [id] => 20
        [news_title] => Startup finance docs in GitHub
        [news_url] => http://venturebeat.com/2013/03/06/fenwick-west-github/
        [news_root_domain] => venturebeat.com
        [news_category] =>
        [news_submitter] => 4
        [news_time] => 2013-03-06 11:20:03
        [news_points] => 0
    )
    [1] => stdClass Object (
        [id] => 21
        [news_title] => The problems with righteous investing
        [news_url] => http://gigaom.com/2013/03/07/the-problems-with-righteous-investing/
        [news_root_domain] => gigaom.com
        [news_category] =>
        [news_submitter] => 4
        [news_time] => 2013-03-08 09:14:17
        [news_points] => 0
    )
)

我将如何在其中访问诸如news_url之类的内容?我已经尝试过了,但是无济于事:

How would I access something like news_url in these? I've tried this, but to no avail:

print_r $this->$record[0]->news_title;

推荐答案

尝试一下:

 $arr =    Array();

    $obj0 = new stdClass;
    $obj0->id = 123;
    $obj0->news_title = "some title 0";
    //etc...
    $obj1 = new stdClass;
    $obj1->id = 124;
    $obj1->news_title = "some title 1";
    //etc...

   $arr[0] = $obj0;
   $arr[1] = $obj1;

    print_r($arr);

或类似

print_r($arr[0]);

甚至

 echo $arr[0]->id;

这篇关于访问数组中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆