I am currently trying to process a relatively large log file which stores game information in JSON. Any log file averagly ranges from 2000-5000 lines.
Example output (retaining formatting…) from the logfile, note: I have removed the players name, SteamID and Ip Address for his own privacy:
{"timestamp": "2012-09-16 14:10:36", "event": "log_start", "unixTime": 1347768636}
{"timestamp": "2012-09-16 14:10:36", "event": "player_status", "player": {"name": "PLAYER NICKNAME", "userId": 15, "uniqueId": "STEAM_X:X:XXXXXXX (STEAMID)", "team": 1}, "address": "xxx.xxx.xxx.xxx (IP)", "country": "AU"}
{"timestamp": "2012-09-16 14:10:36", "event": "live_on_3", "map": "de_mirage_csgo", "teams": [{"name": "Terrorists", "team": 2}, {"name": "Counter-Terrorists", "team": 3}], "status": 5, "version": "1.0.0 Beta"}
This is where the problem begins. Each line of the log file effectively defines a new JSON object? I am trying to decode all of these objects (storing them as associative arrays) line by line (json_decode($string,TRUE) and from there on I try to merge them into one PHP array through a loop. This does ‘work’ (in the sense there is no visual error), but array_merge doesn’t produce anything near the results I expected. This is my code:
/* parse the logfile into array */
for ($i = 0; $i < $lineCount; $i++) {
if ($i === 0) { // first iteration
$arrLog = json_decode($line[$i],true);
} else {
$arrTempLog = json_decode($line[$i],true); // create temp array to store each line
$arrLog = array_merge($arrLog, $arrTempLog); // merge temp array w/ main array
}
}
Any suggestions or input which would lead me in the right direction to achieving what I need would be greatly appreciated!
Thanks
Please clarify your question if this isn’t what you are looking for.
If you want an array of arrays of that Json data, you can do
This will read the entire file into an array line by line and apply
json_decodeto each line/element, resulting in an array of arrays of that Json data.For your Json above, the result would be (demo)
Further information:
file— Reads entire file into an arrayarray_map— Applies the callback to the elements of the given arrays