L
L
Lev Rozanov2020-04-17 20:11:54
PHP
Lev Rozanov, 2020-04-17 20:11:54

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

Good day!

There is an array from POST

<input type="checkbox" name="answer[$id][]" value="$id_2">


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


Did it like this:
$answer = $_POST['answer'];
$answer = array_map('intval', $answer);


For some reason it outputs:
array(1) {
  [56]=>
  int(1)
}


For array_map I tried to insert $_POST itself. The same conclusion.

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

Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2020-04-17
@MetisKot

For some reason it comes out...

Because you have an array within an array, [56 => ['197', '200']]. Here you are in the map and get intval(['197', '200']).

A
Andrey, 2020-04-18
@AndryG

array_walk_recursive _

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question