What are some best practices for handling query results as arrays in PHP to improve code readability and maintainability?

When handling query results as arrays in PHP, it is important to follow best practices to improve code readability and maintainability. One way to achieve this is by using meaningful variable names and organizing the code in a clear and structured manner. Additionally, consider using functions or classes to encapsulate the logic for processing query results, making the code easier to understand and maintain.

// Example of handling query results as arrays in PHP with improved readability and maintainability

// Fetch query results as an associative array
$queryResults = [
    ['id' => 1, 'name' => 'John Doe'],
    ['id' => 2, 'name' => 'Jane Smith']
];

// Process query results using a function
function processQueryResults(array $results): void {
    foreach ($results as $row) {
        echo "ID: {$row['id']} - Name: {$row['name']}\n";
    }
}

// Call the function to process the query results
processQueryResults($queryResults);