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?

Essex Steph
  • 115
  • 1
  • 11

2 Answers2

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

Mubin
  • 4,325
  • 5
  • 33
  • 55
  • Thank you, `$r -> {'Qu1[SQ001]'}` worked. I've not come across this syntax before - can you point me towards an explanation? – Essex Steph Nov 05 '15 at 23:50
  • @EssexSteph: can't find much explanations, look here http://stackoverflow.com/questions/10333016/how-to-access-object-properties-with-names-like-integers – Mubin Nov 05 '15 at 23:56
0

Try:

$r -> Qui[SQ001]

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