What potential pitfalls can arise when working with arrays from a SQL database in PHP?

When working with arrays from a SQL database in PHP, one potential pitfall is that the array keys may not be consistent across different database queries. To solve this, you can use the array_values() function to reindex the array keys numerically.

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

// Fetch data into an associative array
$data = mysqli_fetch_all($result, MYSQLI_ASSOC);

// Reindex array keys numerically
$data = array_values($data);

// Now you can safely iterate over the array
foreach ($data as $row) {
    // Do something with each row
}