j_s_stack
j_s_stack

Reputation: 665

PHP Get variable value out of Object

I got following invalid code: (e.g. $column->Field == 'email')

echo $row[$column->Field];

With the Error:

Fatal error: Cannot use object of type stdClass as array

Thats the var_dump of $row:

object(stdClass)[17]
   public 'id' => string '1' (length=1)
   public 'email' => string 'master' (length=9)
   public 'Name' => string 'THE MASTER' (length=28)
   public 'reply' => string '1' (length=1)

I now what the error means i just can'T figure out how to work around it (i Might be too tired)

Im looking for something like that: What is the correct/working way to do so?

echo $row->$column->Field;

IDK how i didnt got to that earlier but i just defined a variable before hand

$field = $column->Field
echo $row->$field;

Upvotes: 1

Views: 42

Answers (1)

j_s_stack
j_s_stack

Reputation: 665

So 2 Solutios to this one:

1) Define a Variable:

$field = $column->Field;
echo $row->$field;

2) Credit to Abdo Adel:

If you want to do it in one line, try

$row->{$column->Field} 

Upvotes: 2

Related Questions