In what scenarios should variables be stored in arrays or objects rather than numbered?

Variables should be stored in arrays or objects rather than numbered when you have a collection of related data that needs to be grouped together. Arrays and objects allow you to organize and access multiple variables using keys or property names, making your code more organized and readable. This is especially useful when dealing with complex data structures or when you need to pass multiple values to functions or methods.

// Storing variables in an array
$user = [
    'name' => 'John Doe',
    'age' => 30,
    'email' => 'john.doe@example.com'
];

echo $user['name']; // Output: John Doe

// Storing variables in an object
$user = new stdClass();
$user->name = 'John Doe';
$user->age = 30;
$user->email = 'john.doe@example.com';

echo $user->name; // Output: John Doe