<?php
/**
* Binds form field arguments for a quick prototype.
*
* @param array $post Data array representing the form data.
* @param array $defaults Array of default values for each field.
* @return array Modified data array with default values applied.
*/
function bindFormArguments(array $post, array $defaults): array
{
// Apply default values to the post data.
foreach ($defaults as $field => $defaultValue) {
if (isset($post[$field])) {
// If the field exists in the post data, use its value.
continue;
} else {
// Otherwise, use the default value.
$post[$field] = $defaultValue;
}
}
return $post;
}
// Example usage:
// Sample form data (simulating data from a form submission)
$formData = [
'name' => 'John Doe',
'email' => 'john.doe@example.com',
];
// Default values for the form fields.
$defaultValues = [
'name' => 'Guest',
'email' => '',
'age' => 0,
'city' => 'Unknown',
];
// Bind the form arguments.
$boundData = bindFormArguments($formData, $defaultValues);
// Print the bound data.
print_r($boundData);
?>
Add your comment