Arrays - foreach brings & # 8594; Fatal error: you cannot use an object of type

So, I am angry about this array, the 2nd day gives me pain in * ....

I am developing an OOP PHP script.

I get an array:

Array ( [0] => Project Object ( [project_id] => 1 [title] => Some Name [date] => 2011-10-20 [place] => Some City [customer] => 1 [proj_budget] => [manager] => 1 [team] => 1 [currency] => 1 ) ) 

When I try to do this:

 <?php $project = new Project(); $projects = $project->findAll(); print_r($projects); foreach ($projects as $temptwo) { echo $temptwo['title'].", \n"; } ?> 

I get this:

 Fatal error: Cannot use object of type Project as array 

Why in the world? what does he want from me?

+4
source share
5 answers

You get access to elements in the form of arrays

 echo $temptwo['title'].", \n"; 

You probably want to access your properties

 echo $temptwo->title.", \n"; 
+9
source

This is because you are looping an array of objects, so each element in your array is an object that you will need to address as an object.

 foreach($projects as $temptwo){ echo $temptwo->title; } 
+5
source

He wants you to use the object as an object, not an array.

  echo $temptwo->title . ", \n"; 
+2
source

Try using:

 echo $temptwo->title.", \n"; 

instead.

+2
source

You are trying to get object data. It looks like an array, but it is not. this is an object so you need to use

$ temptwo-> name

+1
source

All Articles