function makeArray($keys, $value) {
$result = [];
if (sizeof($keys) == 1) {
$key = $keys[0];
$result[$key] = $value;
} else {
$key = array_shift($keys);
$result[$key] = makeArray($keys, $value);
}
return $result;
}
$path = 'http://api.warface.ru/user/stat/?name=Элез&server=1';
$content = file_get_contents($path);
$content = json_decode($content, TRUE);
$fullResponse = $content['full_response'];
$fullResponse = explode("\n", $fullResponse);
$result = [];
foreach ($fullResponse as $string) {
$string = preg_split('/[\s]*=[\s]*/u', $string, -1, PREG_SPLIT_NO_EMPTY);
if ($string) {
preg_match_all('/[\s]*\[([^\[\]]+)\]([^\[\]]+)/', $string[0], $matches);
$keys = [];
$i = 1; $total = \sizeof($matches);
for ($i; $i < $total; $i+=2) {
$keys = [];
foreach ($matches[$i] as $num => $key) {
$keys[] = $key;
$keys[] = $matches[$i+1][$num];
}
$array = makeArray($keys, $string[1]);
$result = array_merge_recursive($result, makeArray($keys, $string[1]));
}
}
}
$content['full_response'] = $result;
echo '<pre>';
var_dump($content);
echo '</pre>';
function format(string $data): array
{
$make = function (array & $result, string $key, int $value) {
$keys = explode(' ', $key);
$last = array_pop($keys);
while ($current = array_shift($keys))
{
if (!array_key_exists($current, $result)) {
$result[$current] = [];
}
$result = & $result[$current];
}
$result[$last] = + $value;
};
$items = array_filter(array_map('trim', explode('<Sum>', $data)));
$result = [];
foreach ($items as $item) {
$item = str_replace(['[', ']'], ['', ' '], $item);
[$key, $value] = explode(' = ', $item);
$make($result, $key, +$value);
}
return $result;
}
<Sum> [stat]player_ammo_restored = 425339
<Sum> [stat]player_climb_assists = 17154
<Sum> [stat]player_climb_coops = 9321
<Sum> [stat]player_damage = 489573500
<Sum> [mode]PVE [stat]player_deaths = 22793
<Sum> [mode]PVP [stat]player_deaths = 107455
<Sum> [class]Rifleman [mode]PVP [stat]player_headshots = 38150
<Sum> [class]Medic [mode]PVE [stat]player_headshots = 40388
<Sum> [class]Medic [mode]PVP [stat]player_headshots = 4271
<Sum> [class]Recon [mode]PVE [stat]player_headshots = 129160
<Sum> [class]Recon [mode]PVP [stat]player_headshots = 5423
....
const arr = json.full_response.split('\n');
/*
[0 … 99]
0: "<Sum> [stat]player_ammo_restored = 425339"
1: "<Sum> [stat]player_climb_assists = 17154"
2: "<Sum> [stat]player_climb_coops = 9321"
3: "<Sum> [stat]player_damage = 489573500"
4: "<Sum> [mode]PVE [stat]player_deaths = 22793"
5: "<Sum> [mode]PVP [stat]player_deaths = 107455"
6: "<Sum> [class]Rifleman [mode]PVP [stat]player_headshots = 38150"
7: "<Sum> [class]Medic [mode]PVE [stat]player_headshots = 40388"
8: "<Sum> [class]Medic [mode]PVP [stat]player_headshots = 4271"
9: "<Sum> [class]Recon [mode]PVE [stat]player_headshots = 129160"
10: "<Sum> [class]Recon [mode]PVP [stat]player_headshots = 5423"
11: "<Sum> [class]Engineer [mode]PVE [stat]player_headshots = 137344"
12: "<Sum> [class]Engineer [mode]PVP [stat]player_headshots = 17705"
13: "<Sum> [class]Rifleman [mode]PVE [stat]player_headshots = 429473"
*/
arr.forEach((el) => {
if (el.indexOf('Medic') >= 0) {
window.console.log(el);
}
});
/*
<Sum> [class]Medic [mode]PVE [stat]player_headshots = 40388
<Sum> [class]Medic [mode]PVP [stat]player_headshots = 4271
<Sum> [class]Medic [mode]PVP [stat]player_hits = 286872
<Sum> [class]Medic [mode]PVE [stat]player_hits = 824486
<Sum> [class]Medic [mode]PVE [stat]player_melee_headshots = 133
<Sum> [class]Medic [mode]PVP [stat]player_melee_headshots = 37
<Sum> [class]Medic [mode]PVE [stat]player_playtime = 17714490
<Sum> [class]Medic [mode]PVP [stat]player_playtime = 12876754
*/