sanders
sanders

Reputation: 10888

Looping through objects

Suppose you have this array with objects:

array
0 => 
object(User\Entity\User)[297]
  public 'id' => int 1
  private 'first_name' => string 'Peter' (length=5)
  private 'last_name' => string 'Johnson' (length=7)
  private 'initials' => string 'J.J.' (length=4)
  private 'email' => string '[email protected]' (length=21)
 1 => 
object(User\Entity\User)[296]
  public 'id' => int 2 
  private 'first_name' => string 'Edith' (length=8)
  private 'last_name' => string 'Peters' (length=7)
  private 'initials' => string 'R.J.' (length=4)
  private 'email' => string '[email protected]' (length=26)

Now i want to put them in a table. But since i want to make it universal i try to do it on an abstract way.

I have a $aColnames array in the function below in to be able to only show the get the values of the fields i want to see in the table.

This is the method i am trying to build:

private function generateTable()
{

    foreach($this->aData as $aData){
      $this->sTable.= '<tr>';
      foreach($this->aColnames as $sColname){
          $this->sTable.= '<td>';
/****What code goes here ****/
              $this->sTable.= '</td>';
      }
          $this->sTable.= '</tr>';
        }
    }

The question is no how do i get the values from the objects? Do i need to instantiate the objects each time? Can anyone help me out please?

Upvotes: 1

Views: 61

Answers (1)

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 174957

No, you do not need to instantiate the objects again. They are instantiated when you placed them in the array in the first place!

You will however need to either declare these variables public, so that they may be accessed, or have some sort of getter functions.

Upvotes: 1

Related Questions