How to transform data from a textarea into an ordered array?

0

I have a textarea that receives data like the following (example):

01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20

How can I transform each line of this textarea into an array with braces, in an organized way, and taking the 0's before the numbers (01, 02, 03 [...])?

I've tried array_values(array_filter(explode(PHP_EOL, $_POST['textarea']))) but I'm getting a result like the following:

array (size=5)
0 => string '01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20' (length=59)
1 => string '01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20' (length=59)
2 => string '01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20' (length=59)
3 => string '01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20' (length=59)
4 => string '01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20' (length=59)

This way there are no keys corresponding to each number.

The expected return would look something like this:

var_dump($array[0])

array (size=20)
  0 => string '1' (length=1)
  1 => string '2' (length=1)
  2 => string '3' (length=1)
  3 => string '4' (length=1)
  4 => string '5' (length=1)
  5 => string '6' (length=1)
  6 => string '7' (length=1)
  7 => string '8' (length=1)
  8 => string '9' (length=1)
  9 => string '10' (length=2)
  10 => string '11' (length=2)
  11 => string '12' (length=2)
  12 => string '13' (length=2)
  13 => string '14' (length=2)
  14 => string '15' (length=2)
  15 => string '16' (length=2)
  16 => string '17' (length=2)
  17 => string '18' (length=2)
  18 => string '19' (length=2)
  19 => string '20' (length=2)

This for each line of the textarea.

    
asked by anonymous 31.10.2017 / 02:55

2 answers

2

Although ugly but functional, it improves there:

<?php 
if(isset($_POST['numeros'])){
    $resultado = array();
$linhas = explode(PHP_EOL, $_POST['numeros']);
    foreach ($linhas as $linha){
        $numeros = explode(" ", $linha);
            $intVal = array();
            foreach ($numeros as $numero){
                array_push($intVal, intval($numero));
            }
        array_push($resultado, $intVal);
    }
}
print_r($resultado);
?>
<hr>
<form action="index.php" method="post">
    <textarea name="numeros" cols="150" rows="15"></textarea>
    <button type="submit">Enviar</button>
</form>

The result was;

[0] => Array (
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
    [10] => 11
    [11] => 12
    [12] => 13
    [13] => 14
    [14] => 15
    [15] => 16
    [16] => 17
    [17] => 18
    [18] => 19
    [19] => 20
    )
[1] => Array (
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
    [10] => 11
    [11] => 12
    [12] => 13
    [13] => 14
    [14] => 15
    [15] => 16
    [16] => 17
    [17] => 18
    [18] => 19
    [19] => 20
    )
[2] => Array (
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
    [10] => 11
    [11] => 12
    [12] => 13
    [13] => 14
    [14] => 15
    [15] => 16
    [16] => 17
    [17] => 18
    [18] => 19
    [19] => 20
    )
[3] => Array (
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
    [10] => 11
    [11] => 12
    [12] => 13
    [13] => 14
    [14] => 15
    [15] => 16
    [16] => 17
    [17] => 18
    [18] => 19
    [19] => 20
    )
[4] => Array (
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
    [10] => 11
    [11] => 12
    [12] => 13
    [13] => 14
    [14] => 15
    [15] => 16
    [16] => 17
    [17] => 18
    [18] => 19
    [19] => 20
    )
    
31.10.2017 / 03:37
2

One way to get this result is by looping loops:

$saida = array();
$tmp = explode(PHP_EOL, $_POST['textarea']);

foreach ($tmp as $i) {
  $i = explode(' ', $i);
  for ($j = 0; $j < count($i); $j++) {
    $i[$j] = strval(intval($i[$j]));
  }
  array_push($saida, $i);
}

var_dump($saida[0]); //exemplo de saída conforme demonstrado na pergunta

To remove leading zeros, transform the string into an integer using intval() . Because, for the example of the question, you need the data to be strings, you can convert them back using strval() / a>.

Example on repl.it: link

    
31.10.2017 / 03:26