I have in my $_POST
array a variable without keys: (This is from an input form with name[]
).
var_dump($_POST)
looks like this:
array(2) {
["name1"]=>
string(1) "ttt"
["name2"]=>
array(1) {
[0]=>
string(2) "test"
}
}
How can I correctly use filter_input_array
on this array?
For example, I need use FILTER_VALIDATE_INT
to all name2
elements.
UPD: print_r
of $_POST
:
Array
(
[name1] => ttt
[name2] => Array
(
[0] => test
)
)
$args = array(
'name1' => SOME_FILTER // this is for example,
'name2' => array(
'filter' => FILTER_VALIDATE_INT,
'flags' => FILTER_REQUIRE_SCALAR,
),
);
$myinputs = filter_input_array(INPUT_POST, $args);
var_dump($myinputs); echo " ";
suppose your post of name2
is like this:$_POST['name2']=array(0=>"100",1=>20,2=>"test");
then try the below code.
<?php
$_POST['name2']=array(0=>"100",1=>20,2=>"test");
foreach($_POST['name2'] as $key=>$val){
if (!filter_var($val, FILTER_VALIDATE_INT) === false) {
echo("value is an integer<br>");
} else {
echo("value is not an integer<br>");
}
}
?>