-1

When I run the following code, I get exactly that output that I need.

$array_items = count($links);
for ($i = 0; $i <= $array_items; $i++) {
    echo $links[$i]->title;
}

However, I get 2 notices:

Notice: Undefined offset: 2 in C:\xampp\htdocs...

Notice: Trying to get property of non-object in C:\xampp\htdocs...

I guess that's because $links returns an array and inside that array there is an object.

How can I fix it?

vqdave
  • 2,361
  • 1
  • 18
  • 36
Reza Saadati
  • 5,018
  • 4
  • 27
  • 64

1 Answers1

1

You need to check only if $i is lower than the count because the index start form 0 and the count give you back the total items (+1)

$array_items = count($links);
for ($i = 0; $i < $array_items; $i++) {
    echo $links[$i]->title;
}
Shibi
  • 1,373
  • 2
  • 9
  • 8