1

I've got a list of objects:

 0 => 
    object(stdClass)[550]
      public 'node_title' => string 'Test' (length=4)
      public 'nid' => string '1473' (length=4)
      public 'node_language' => string 'nl' (length=2)
  1 => 
    object(stdClass)[552]
      public 'node_title' => string 'Test2' (length=5)
      public 'nid' => string '1321' (length=4)
      public 'node_language' => string 'nl' (length=2)
  2 => 
    object(stdClass)[553]
      public 'node_title' => string 'Test3' (length=5)
      public 'nid' => string '602' (length=3)
      public 'node_language' => string 'nl' (length=2)

And I loop over them using a foreach loop:

foreach($view->result as $key => $value) {

}

So, if every time the foreach loops, 1 object is fetched, right?
But how can I access one item of the object? I've tried with 0->nid, $key->nid,... but nothing worked... How can I do so?

1
  • 1
    Have a look at the foreach documentation. As the variable names already suggest, $key is the index of the array element, while $value is, surprise, the value. If you don't know which of these to use, you can simply inspect them with print_r($key) and print_r($value). Commented Feb 29, 2012 at 13:32

1 Answer 1

2
foreach($view->result as $key => $value) {
    // Here, $value is the current object
    echo($value->node_title);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Oh, well. This is embarrassing... Thanks though :)
No problem :) Everyone misses something now and again

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.