1. <?php
  2. /**
  3. * Maps configuration values from a temporary configuration array.
  4. *
  5. * @param array $config_values An array of configuration values. Keys are the field names.
  6. * @param array $field_mapping An array mapping field names to their desired values.
  7. * @return array An array of configured values, using the provided field mapping.
  8. */
  9. function mapConfiguration(array $config_values, array $field_mapping): array
  10. {
  11. $configured_values = [];
  12. foreach ($config_values as $field => $value) {
  13. // Check if the field has a mapping
  14. if (isset($field_mapping[$field])) {
  15. $configured_values[$field] = $field_mapping[$field];
  16. } else {
  17. // Use the original value if no mapping is defined.
  18. $configured_values[$field] = $value;
  19. }
  20. }
  21. return $configured_values;
  22. }
  23. // Example Usage:
  24. // $config = ['database_host' => 'localhost', 'debug_mode' => 'false', 'api_key' => ''];
  25. // $mapping = ['database_host' => 'remote_host', 'debug_mode' => 'true'];
  26. // $configured_config = mapConfiguration($config, $mapping);
  27. // print_r($configured_config);
  28. ?>

Add your comment