How can arrays be effectively used in PHP to store and manipulate query results for better performance?

Using arrays in PHP to store and manipulate query results can improve performance by reducing the number of database queries needed and allowing for easier data manipulation. By storing query results in arrays, you can loop through the data multiple times without having to query the database again. This can be especially useful for complex queries or when working with large datasets.

// Example of storing query results in an array for better performance
$query = "SELECT * FROM users";
$result = mysqli_query($connection, $query);

$userData = array();
while ($row = mysqli_fetch_assoc($result)) {
    $userData[] = $row;
}

// Now you can manipulate the $userData array without needing to query the database again
foreach ($userData as $user) {
    echo $user['username'] . "<br>";
}