How can PHP developers effectively map array values to database IDs while preserving the original array keys?

When mapping array values to database IDs while preserving the original array keys, PHP developers can create a new associative array where the keys are the original array keys and the values are the corresponding database IDs. This can be achieved by iterating through the original array and querying the database to retrieve the IDs based on the array values. The retrieved IDs can then be stored in the new associative array with the original keys.

<?php
// Original array with values to map to database IDs
$originalArray = [
    'key1' => 'value1',
    'key2' => 'value2',
    'key3' => 'value3'
];

// New associative array to store database IDs with original keys
$databaseIds = [];

// Simulated database query to retrieve IDs based on array values
$databaseValues = ['value1' => 101, 'value2' => 102, 'value3' => 103];

// Map array values to database IDs while preserving original keys
foreach ($originalArray as $key => $value) {
    $databaseIds[$key] = $databaseValues[$value];
}

// Output the new associative array with database IDs
print_r($databaseIds);
?>