1. <?php
  2. /**
  3. * Deduplicates environment variables. For development use only.
  4. * Synchronous execution.
  5. */
  6. /**
  7. * Deduplicates environment variables.
  8. * @param array $env An array of environment variables (key => value).
  9. * @return array A new array with deduplicated environment variables.
  10. */
  11. function deduplicateEnv(array $env): array
  12. {
  13. $uniqueEnv = [];
  14. $seenValues = [];
  15. foreach ($env as $key => $value) {
  16. // Check if the value has already been seen.
  17. if (!in_array($value, $seenValues)) {
  18. $uniqueEnv[$key] = $value;
  19. $seenValues[] = $value;
  20. }
  21. }
  22. return $uniqueEnv;
  23. }
  24. // Example Usage (replace with your actual environment variables)
  25. $environmentVariables = [
  26. 'DEBUG' => 'true',
  27. 'LOG_LEVEL' => 'INFO',
  28. 'DATABASE_URL' => 'mysql://user:password@host:port/db',
  29. 'DEBUG' => 'false', // Duplicate
  30. 'LOG_LEVEL' => 'WARNING', // Duplicate
  31. 'API_KEY' => 'abcdef123456',
  32. 'DATABASE_URL' => 'mysql://user:password@host:port/db' // Duplicate
  33. ];
  34. $deduplicatedVariables = deduplicateEnv($environmentVariables);
  35. // Print the deduplicated environment variables
  36. print_r($deduplicatedVariables);
  37. ?>

Add your comment