1

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;
3
  • 2
    if you want to do it in one line, try $row->{$column->Field} Commented Jul 5, 2015 at 9:14
  • The error message is pretty clear: $row is not an array (it is an object), yet you try to access it like an array ($row[$column->Field]). That is not possible, it is invalid code. Commented Jul 5, 2015 at 9:16
  • The rest of the question is inclear. What is Field meant to be? Do you mean that as a placeholder for on of the property names? You don't say so... Commented Jul 5, 2015 at 9:19

1 Answer 1

2

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} 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.