0

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,329
  • 14
  • 42
  • 90
  • https://3v4l.org/N8jE4 (you can check older version output as well by clicking eol versions option in the example link) – Anant Kumar Singh Feb 28 '20 at 06:14
  • 1
    `In theory, I should be able to do $arr->title` Where you read this , that when you create a array you will can access as object ? – Niklesh Raut Feb 28 '20 at 06:18

1 Answers1

3

this code work 100% fine

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

echo $arr->title;