What are the common pitfalls to watch out for when working with arrays in PHP that are filled by user input?

One common pitfall when working with arrays filled by user input in PHP is not properly sanitizing the input, which can lead to security vulnerabilities such as SQL injection or cross-site scripting attacks. To mitigate this risk, always validate and sanitize user input before using it in an array.

// Example of sanitizing user input before using it in an array
$user_input = $_POST['user_input']; // assuming user_input is coming from a form submission

// Sanitize user input using htmlspecialchars to prevent XSS attacks
$sanitized_input = htmlspecialchars($user_input);

// Create an array with the sanitized input
$my_array = array($sanitized_input);

// Now you can safely use $my_array without worrying about security vulnerabilities