1. <?php
  2. /**
  3. * Binds form field arguments for a quick prototype.
  4. *
  5. * @param array $post Data array representing the form data.
  6. * @param array $defaults Array of default values for each field.
  7. * @return array Modified data array with default values applied.
  8. */
  9. function bindFormArguments(array $post, array $defaults): array
  10. {
  11. // Apply default values to the post data.
  12. foreach ($defaults as $field => $defaultValue) {
  13. if (isset($post[$field])) {
  14. // If the field exists in the post data, use its value.
  15. continue;
  16. } else {
  17. // Otherwise, use the default value.
  18. $post[$field] = $defaultValue;
  19. }
  20. }
  21. return $post;
  22. }
  23. // Example usage:
  24. // Sample form data (simulating data from a form submission)
  25. $formData = [
  26. 'name' => 'John Doe',
  27. 'email' => 'john.doe@example.com',
  28. ];
  29. // Default values for the form fields.
  30. $defaultValues = [
  31. 'name' => 'Guest',
  32. 'email' => '',
  33. 'age' => 0,
  34. 'city' => 'Unknown',
  35. ];
  36. // Bind the form arguments.
  37. $boundData = bindFormArguments($formData, $defaultValues);
  38. // Print the bound data.
  39. print_r($boundData);
  40. ?>

Add your comment