1

Sorry if this is a particularly stupid question but it's late and I'm going slightly round the bend on this.

I have an object returned from an API which echo '<pre>';print_r($r);echo '</pre>'; gives as:

stdClass Object ( [id] => 49 [submitdate] => 2015-11-05 14:33:16 [lastpage] => 4 [startlanguage] => en [Qu1[SQ001]] => Fred Bloggs [Qu1[SQ003]] => Caretaker [Feedback] => Great course, thank you ) 

The line echo '<p>Feedback: '.$r -> Feedback.'</p>'; displays 'Great course, thank you' as expected, but echo '<p>'.$r -> Qu1 -> SQ001.'</p>'; doesn't show 'Fred Bloggs', instead I get an error: Notice: Undefined property: stdClass::$Qu1.

Please will somebody tell me what I'm doing wrong?

6
  • Possible duplicate of php how to access object array Commented Nov 5, 2015 at 23:14
  • As explained above, that works for $r -> Feedback (and $r -> id), but not if I try and get the name (Fred Bloggs), I expected it to be $r -> Qu1 -> SQ001, but I get the error as described. Commented Nov 5, 2015 at 23:27
  • Try $r->Qu1['SQ001'] Commented Nov 5, 2015 at 23:30
  • Sorry, still get the same error Notice: Undefined property: stdClass::$Qu1 Commented Nov 5, 2015 at 23:34
  • How do you get this array? Commented Nov 5, 2015 at 23:37

2 Answers 2

1

you can do it in two ways(may be there are more, but I know two only :))

1)

$obj['a'] = "A"; $obj['b'] = "B"; $obj['c'] = "C"; $obj['get[d]'] = "D"; $obj['get[E]'] = "E"; $obj = (object) $obj; echo "This is: ".$obj->{'get[d]'}.PHP_EOL; echo "This is: ".$obj->{'get[E]'}.PHP_EOL; 

will output

This is: D This is: E 

2)

other is, you can get the properties of object by using get_object_vars() states

get_object_vars — Gets the properties of the given object

So,

$array = get_object_vars($obj); echo "This is: ".$array['get[E]']." but getting from array"; 

DEMO

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, $r -> {'Qu1[SQ001]'} worked. I've not come across this syntax before - can you point me towards an explanation?
@EssexSteph: can't find much explanations, look here stackoverflow.com/questions/10333016/…
0

Try:

$r -> Qui[SQ001]

Qui looks to be an array so $r -> Qui -> SQ001 won't work

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.