How can the implode function be used effectively to streamline multiple SQL inserts in PHP?

When inserting multiple rows into a database using SQL in PHP, it can be cumbersome to write out each individual insert statement. The implode function can be used effectively to streamline this process by concatenating values into a single string, reducing the number of queries sent to the database. This can improve performance and make the code more readable.

// Sample array of data to be inserted
$data = [
    ['John', 'Doe', 'john.doe@example.com'],
    ['Jane', 'Smith', 'jane.smith@example.com']
];

// Implode the values for each row
$insertValues = [];
foreach ($data as $row) {
    $insertValues[] = "('".implode("', '", $row)."')";
}

// Construct the SQL query
$sql = "INSERT INTO users (first_name, last_name, email) VALUES ".implode(", ", $insertValues);

// Execute the query
$result = mysqli_query($conn, $sql);

if ($result) {
    echo "Data inserted successfully";
} else {
    echo "Error inserting data: " . mysqli_error($conn);
}