Organize information

0

I have text like this:

NICK:KEOME PTS:50 ASTS:6 DEFREB:2 OFFREB:7 STLS:14 BLKS:4 TOS:5 NICK:ARTHUR PTS:10 ASTS:3 DEFREB:5 OFFREB:4 STLS:4 BLKS:2 TOS:6

How do I store information more or less this way:

Jogadores (
  [nome] => Keome
  [stats] => (
      [pts] => 50
      [asts] => 6
      [dreb] => 2
      [oreb] => 7
      [stls] => 14
      [blks] => 4
      [tos] =>  5
  )
  [nome] => Art
  [stats] => (
      [pts] => 10
      [asts] => 3
      [dreb] => 2
      [oreb] => 1
      [stls] => 4
      [blks] => 5
      [tos] =>  6
  )
)

I need names related to "stats", but each value is separate. Well, I need to play those values in the database. I tried to use this code but I did not succeed.

$linha = 'NICK:KEOME PTS:50 ASTS:6 DEFREB:2 OFFREB:7 STLS:14 BLKS:4 TOS:5 NICK:ARTHUR PTS:10 ASTS:3 DEFREB:5 OFFREB:4 STLS:4 BLKS:2 TOS:6';
  $splitted = explode(" ", $linha);


  $arrN = array(
  );

  foreach ($splitted as $item) {
    $valor = explode(':', $item);
    $arrN[$valor[0]] = $valor[1];
  }
    
asked by anonymous 30.05.2017 / 20:46

1 answer

3

You can use regular expressions to extract the data:

<?php

// Dados de entrada:
$linha = 'NICK:KEOME PTS:50 ASTS:6 DEFREB:2 OFFREB:7 STLS:14 BLKS:4 TOS:5 NICK:ARTHUR PTS:10 ASTS:3 DEFREB:5 OFFREB:4 STLS:4 BLKS:2 TOS:6';

// Verifica se é possível obter os dados com expressão regular:
if (preg_match_all("/([a-zA-Z]+)\:([a-zA-Z0-9]+)\s?/", $linha, $matches)) {

  // Lista de jogadores:
  $players = [];

  // Dados do jogador:
  $player = [];

  // Percorre todos os valores da entrada:
  foreach(array_map(null, $matches[1], $matches[2]) as $pair) {

    // O valor é um nick?
    if (strtoupper($pair[0]) == "NICK") {

      // Se houver dados de outro jogador, adicione a lista:
      if ($player) {
        $players[] = $player;
      }

      // Inicia os dados do jogador para o nick atual:
      $player = ["name" => $pair[1], "stats" => []];

    } else {

      // Não é um nick, então só pode ser um status do jogador atual:
      $player["stats"][strtolower($pair[0])] = $pair[1];
    }
  }

  // Adiciona à lista os dados do último jogador:
  if ($player) {
    $players[] = $player;
  }

  // Exibe a lista de jogadores completa:
  print_r($players);
}
  

See working at Ideone .

The output generated is:

Array
(
    [0] => Array
        (
            [name] => KEOME
            [stats] => Array
                (
                    [pts] => 50
                    [asts] => 6
                    [defreb] => 2
                    [offreb] => 7
                    [stls] => 14
                    [blks] => 4
                    [tos] => 5
                )

        )

    [1] => Array
        (
            [name] => ARTHUR
            [stats] => Array
                (
                    [pts] => 10
                    [asts] => 3
                    [defreb] => 5
                    [offreb] => 4
                    [stls] => 4
                    [blks] => 2
                    [tos] => 6
                )

        )

)

Explanation

With the regular expression /([a-zA-Z]+)\:([a-zA-Z0-9]+)\s?/ , we get all the groups that follow the chave:valor pattern, that is, a name, called a key, followed by a colon and a value. All of these values are grouped in the array $matches , containing a 1 index with all names and a 2 index with all values:

// print_r($matches);

Array
(
    [0] => Array
        (
            [0] => NICK:KEOME 
            [1] => PTS:50 
            [2] => ASTS:6 
            [3] => DEFREB:2 
            [4] => OFFREB:7 
            [5] => STLS:14 
            [6] => BLKS:4 
            [7] => TOS:5 
            [8] => NICK:ARTHUR 
            [9] => PTS:10 
            [10] => ASTS:3 
            [11] => DEFREB:5 
            [12] => OFFREB:4 
            [13] => STLS:4 
            [14] => BLKS:2 
            [15] => TOS:6
        )

    [1] => Array
        (
            [0] => NICK
            [1] => PTS
            [2] => ASTS
            [3] => DEFREB
            [4] => OFFREB
            [5] => STLS
            [6] => BLKS
            [7] => TOS
            [8] => NICK
            [9] => PTS
            [10] => ASTS
            [11] => DEFREB
            [12] => OFFREB
            [13] => STLS
            [14] => BLKS
            [15] => TOS
        )

    [2] => Array
        (
            [0] => KEOME
            [1] => 50
            [2] => 6
            [3] => 2
            [4] => 7
            [5] => 14
            [6] => 4
            [7] => 5
            [8] => ARTHUR
            [9] => 10
            [10] => 3
            [11] => 5
            [12] => 4
            [13] => 4
            [14] => 2
            [15] => 6
        )
)

To group each key with its value, we use the function array_map .

// print_r(array_map(null, $matches[1], $matches[2]));

Array
(
    [0] => Array
        (
            [0] => NICK
            [1] => KEOME
        )

    [1] => Array
        (
            [0] => PTS
            [1] => 50
        )

    [2] => Array
        (
            [0] => ASTS
            [1] => 6
        )

    [3] => Array
        (
            [0] => DEFREB
            [1] => 2
        )

    [4] => Array
        (
            [0] => OFFREB
            [1] => 7
        )

    [5] => Array
        (
            [0] => STLS
            [1] => 14
        )

    [6] => Array
        (
            [0] => BLKS
            [1] => 4
        )

    [7] => Array
        (
            [0] => TOS
            [1] => 5
        )

    [8] => Array
        (
            [0] => NICK
            [1] => ARTHUR
        )

    [9] => Array
        (
            [0] => PTS
            [1] => 10
        )

    [10] => Array
        (
            [0] => ASTS
            [1] => 3
        )

    [11] => Array
        (
            [0] => DEFREB
            [1] => 5
        )

    [12] => Array
        (
            [0] => OFFREB
            [1] => 4
        )

    [13] => Array
        (
            [0] => STLS
            [1] => 4
        )

    [14] => Array
        (
            [0] => BLKS
            [1] => 2
        )

    [15] => Array
        (
            [0] => TOS
            [1] => 6
        )
)

See that the result is a list of pairs, where index 0 is the name and index 1 is its value. After, we iterate on this list, following the logic that if found the name NICK , a new player will be described, or if it is any other value, it should be considered as a current player status.

    
30.05.2017 / 23:50