How can the output of a MySQL query be formatted into a more organized array in PHP?

When fetching data from a MySQL database in PHP, the output may not be in a structured array format, which can make it difficult to work with. To format the output into a more organized array, you can use the `fetch_assoc()` function in a while loop to fetch each row as an associative array. This will allow you to access the data by column name, making it easier to manipulate and display.

// Connect to MySQL database
$conn = new mysqli("localhost", "username", "password", "database");

// Query to fetch data
$result = $conn->query("SELECT * FROM table");

// Initialize empty array to store formatted data
$data = array();

// Fetch each row as an associative array
while ($row = $result->fetch_assoc()) {
    $data[] = $row;
}

// Display formatted data
print_r($data);

// Close database connection
$conn->close();