How to convert array of strings into an array of numbers?

Good day!

There is an array of the POST
<input type="checkbox" name="answer[$id][]" value="$id_2">


Turns a string array:
array(1) {
[56]=>
 array(2) {
[0]=>
 string(3) "197"
[1]=>
 string(3) "200"
}
}


Did
$answer = $_POST['answer'];
$answer = array_map('intval', $answer);


Displays the reason:
array(1) {
[56]=>
int(1)
}


For array_map tried to insert himself $_POST. The same conclusion.

Also, tried to write down in the value of the checkbox, intval($id_2), but still writes deadline.

Thanks in advance!
April 19th 20 at 12:35
2 answers
April 19th 20 at 12:37
Solution
Displays for some reason...

Because you have array in array, [56 => ['197', '200']]. Here you are in map e and get intval(['197', '200']).
@Alden.Morissette thank you for your answer! And how then to convert the array to int array? - mikel_Douglas commented on April 19th 20 at 12:40
@mikel_Douglas, But it really need? PHP (and JS) is quite loyal to the.
print 1+"1"; // 2
print "2"*"3"; // 6
print "2" == 2; // 1

But if you really want, you have to do a nested array_map.
$answer = array_map(
 function ($el) {
 return array_map('intval', $el);
},
$_POST['answer']
);
- Alden.Morissette commented on April 19th 20 at 12:43
@Alden.Morissette, thank you so much! - mikel_Douglas commented on April 19th 20 at 12:46
@Alden.Morissette, and I'd be like a donkey, made a recursive function... I look Like an ass? - eloy.Paucek3 commented on April 19th 20 at 12:49
April 19th 20 at 12:39

Find more questions by tags PHP