My Problem is to push the some Content to a JSON-File in the right way through PHP. I have written some Code, but it won't work.
Here is my Code:
//Get Form Data
$formdata_host = array (
'server' => array ( $Server => array(
array (
'svc' => $_POST['valservice'],
'id'=> 1
)
))
);
//Get data from existing json file
$jsondata = file_get_contents($filename_moni);
//converts json data into array
$arr_data = json_decode($jsondata, true);
//Push details data to array
array_push($arr_data,$formdata_host);
//Reindex the Array
$arr_data = array_values($arr_data);
//Convert updated array to JSON
$jsondata = json_encode($arr_data, JSON_PRETTY_PRINT|JSON_NUMERIC_CHECK);
//write json data into data.json file
if(file_put_contents($filename_moni, $jsondata)) {
echo 'Daten erfolgreich gespeichert!';
}
else
echo "Error";
}
catch (Exception $e) {
echo 'Ausnahme entdeckt: ', $e->getMessage(), "\n";
}
This is the JSON-Content what i am getting after executing:
[
{
"server": {
"TEST": [
{
"svc": "TEST",
"id": 1
}
]
}
}
]
But i need this:
{
"server": {
"TESTSERVER": [
{"svc":"TESTSERVICE", "id":1}
]
}
}
I know that the [] is for array and the {} is for an Object. I need first a JSON-Object -> server, following by a second JSON-Object -> hostname and then a JSON-Array following by several JSON-Objects filled with Servicenames and IDs.
I hope, you can help me, cause this Issue is driving me crazy right now.