I want to validate and sanitize data which comes from POST
array.
My POST
data is something like this:
Array
(
[category_name] => fsdfsfwereq34
[subCategory] => Array
(
[0] => sdfadsffasfasdf
[1] => sdfasfdsafadsf
[2] => safdfdasfas
)
[category-submitted] => TRUE
)
1
I can validate and sanitize category_name
and this is how I do it in PHP.
if (!empty( $_POST['category_name'])) {
$category = filter_input(INPUT_POST, 'category_name', FILTER_SANITIZE_STRING);
} else {
$category = NULL;
}
But I do not know how to do it for values of the subCategory
array.
Can anybody tell me how can I do this?
Hope somebody may help me out. Thank you.
You can use foreach to loop through all values of an array. In the loop you can push your filtered values to a new array $subCategory (e.g.).
E.g.:
$subCategory = $_POST['subCategory'];
$subcategories = array();
if (!empty( $subCategory ) && is_array( $subCategory ) ) {
foreach( $subCategory as $key => $value ) {
$subCategories[] = filter_var( $value, FILTER_SANITIZE_STRING )
}
}