How can arrays be combined in PHP to streamline the process of inserting data into a database with multiple columns?

When inserting data into a database with multiple columns in PHP, combining arrays can streamline the process by allowing you to match keys in the array with column names in the database table. This way, you can easily insert data into the appropriate columns without having to manually specify each column name.

// Sample arrays with data to be inserted
$data = [
    'name' => 'John Doe',
    'email' => 'john.doe@example.com',
    'age' => 30
];

// Combine arrays to match keys with column names
$columns = implode(', ', array_keys($data));
$values = ':' . implode(', :', array_keys($data));

// Prepare SQL statement with placeholders
$sql = "INSERT INTO users ($columns) VALUES ($values)";

// Prepare and execute the query with the combined array
$stmt = $pdo->prepare($sql);
$stmt->execute($data);