I have a file on a server that I need to parse and build a JSON object to return. I am using PHP.
The file contents look something like this:
########################################
# NOTES FILE
#
# THIS FILE IS AUTOMATICALLY GENERATED
# DO NOT MODIFY THIS FILE!
########################################
info {
created=1552596653
version=4.4.3
last_update_check=1552554585
update_available=0
last_version=4.4.3
new_version=4.4.3
}
programstatus {
modified_host_attributes=0
modified_service_attributes=0
pid=11523
daemon_mode=1
program_start=1552593834
last_log_rotation=0
...
Ideally, I would like to grab EACH segment (eg: "info", "programstatus", etc...) and add them to the JSON obj/array as I parse through it. With each attribute = value
being assigned accordingly.
So something like:
$data = array();
// Loop here for each segment
$data['info'] = array(
"created" => "1552596653",
"version" => "4.4.3",
etc...
)
// Then wrap it up with something like
return json_encode($data);
I just can't "think" to loop through the file while breaking it out into chunks.
I have the file contents via:
$statusFile = '/location/to/my/data/file';
ob_start();
include( $statusFile );
$statusFileContent = ob_get_contents();
ob_end_clean();