How can arrays be utilized effectively in PHP to sort and manipulate data retrieved from a MySQL database?

Arrays can be utilized effectively in PHP to sort and manipulate data retrieved from a MySQL database by fetching the data from the database into an array, using PHP array functions to sort and manipulate the data as needed, and then displaying the processed data on the webpage.

// Connect to MySQL database
$connection = mysqli_connect("localhost", "username", "password", "database");

// Fetch data from MySQL database
$query = "SELECT * FROM table";
$result = mysqli_query($connection, $query);

// Store fetched data in an array
$data = [];
while ($row = mysqli_fetch_assoc($result)) {
    $data[] = $row;
}

// Sort the data based on a specific key
usort($data, function($a, $b) {
    return $a['column_name'] <=> $b['column_name'];
});

// Manipulate the data as needed
foreach ($data as $row) {
    // Perform manipulations on $row
}

// Display the processed data
foreach ($data as $row) {
    echo $row['column_name'] . "<br>";
}

// Close database connection
mysqli_close($connection);