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 on 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?/
, all groups following the pattern are obtained chave:valor
, that is, a name, called a key, followed by two points and a value. All these values are grouped in the array $matches
, containing an index 1
with all names and an index 2
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 respective 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
)
)
Note that the result will be a list of pairs, where in index 0 will be the name and in index 1 will be the respective value. After, we iterate on this list, following the logic that if found the name NICK
, a new player will be described, or if any other value should be considered as a status of the current player.
And what would be the "SAN ANTONIO SPURS"?
– Woss
is the team, but an unnecessary information.
– Willian
And follows a pattern? How to differentiate the player name for the team name?
– Woss
in front of the player name I can add a character to differentiate
– Willian
How will you generate this format? You can no longer place it directly on array as desired?
– Woss
I take from a textarea, the problem is that I don’t know how to put in the array, I’ve looked at the documentation but I don’t understand it very well.
– Willian
This data comes to PHP through a form, then?
– Woss
Yes, like this.
– Willian