10

I have the following output:

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
    )
)

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;
1
  • 1
    You probably want: print_r $this->record[0]->news_title;, note I removed the $ in $record[0]. Commented May 7, 2013 at 0:12

2 Answers 2

14

try this:

 $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);

or something like

print_r($arr[0]);

or even

 echo $arr[0]->id;
Sign up to request clarification or add additional context in comments.

Comments

2

You are using class property, you might want to check if it is accessible first. While accessing the class property after using $this you dont need the additional $, just use $this - record. Like

echo $this -> record[0] -> title;

If record is a valid class property which is an array and it still does not work. Give this a try too:

echo {$this -> record[0]} -> title;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.