How can array keys be effectively used to assign specific types, lengths, or value ranges to form input data in PHP?

When dealing with form input data in PHP, array keys can be effectively used to assign specific types, lengths, or value ranges to ensure data integrity and security. By setting rules within the array keys, you can validate and sanitize the input data before processing it further.

// Example of using array keys to define rules for form input data
$input_data = [
    'username' => [
        'type' => 'string',
        'min_length' => 3,
        'max_length' => 20
    ],
    'email' => [
        'type' => 'email'
    ],
    'age' => [
        'type' => 'integer',
        'min_value' => 18,
        'max_value' => 100
    ]
];

// Validate and sanitize the input data based on the rules defined in array keys
foreach ($input_data as $key => $rules) {
    // Implement validation and sanitization logic here
}