1

Say I have an array like the following:

$arr = array(
    'id' => 123,
    'title' => 'Example Title',
);

How come I cannot access the values using PHP's object operator (->)? In theory, I should be able to do $arr->title but that doesn't work and I have to access it as $arr['title'] instead.

I've been reading plenty of examples of people using the object operator however it's not returning anything but a null value.

Has something changed in recent versions of PHP or am I misunderstanding the examples given?

Spedwards
  • 4,167
  • 16
  • 49
  • 106

1 Answers1

4

this code work 100% fine

$arr = (object) array(
    'id' => 123,
    'title' => 'Example Title',
);

echo $arr->title;
  • 1
    Ahh, I was unaware I had to cast the array to an object. Thank you. – Spedwards Feb 28 '20 at 06:18
  • Ahhh, I was unaware that some of my arrays must have been objects, and the object operator worked. "Suddenly" it stopped working and I wondered why. Thanks! – franc Jul 26 '23 at 15:17