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

Asked

Viewed 239 times

0

I have a textarea that takes 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 keys, in an organized way and taking out the "0" before the numbers (01, 02, 03 [...])?

I tried to get 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)

That way there are no keys that match each number.

The expected return would be 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.

  • What is the desired output? You can put an example?

  • I edited the question, Anderson.

2 answers

2

One way to get this result is by chaining 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 the zeros on the left, turn the string into an integer using intval(). As, by the example of the question, you need the data to be strings, you can convert them back using strval().

Example in repl.it: https://repl.it/N3l4/0

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 lump 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
    )
  • Thank you Milrak! It worked as I wanted.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.