What are some common pitfalls when working with arrays of objects in PHP?

One common pitfall when working with arrays of objects in PHP is not properly accessing object properties within the array. To avoid this, make sure to use the arrow operator (->) to access object properties instead of the dot operator (.). Another pitfall is not checking if an object property exists before trying to access it, which can lead to errors. To prevent this, use isset() or property_exists() to check if the property exists before accessing it.

// Incorrect way to access object property in an array
$objects = [
    new stdClass(),
    new stdClass(),
];

foreach ($objects as $object) {
    echo $object->property; // This will cause an error
}
```

```php
// Correct way to access object property in an array
$objects = [
    (object)['property' => 'value1'],
    (object)['property' => 'value2'],
];

foreach ($objects as $object) {
    if (isset($object->property)) {
        echo $object->property;
    }
}