0

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.

danielx78
  • 41
  • 4
  • what value do you have in `$Server` and `$_POST['valservice']`? It seem there is not that much issue with the code but just wrong values if I look at your expected result vs current result. – Alex Sep 17 '18 at 15:24
  • First, you're getting an array in the output because you are JSON-ifying an array. JSON-ify an object if you want an object. Therefore you need to create an object, create properties on that object that you want output and of the type you want output, then JSON-ify that object. – Matt Runion Sep 17 '18 at 15:38
  • change this `array_push` to `array_merge` or `array_replace` and get rid of this `array_values` Also you are missing the `try` part of the `catch` maybe that is a typo or omission from the question. – ArtisticPhoenix Sep 17 '18 at 15:39

2 Answers2

0

Change these 2 things (as I said in the comments)

//Push details data to array
$arr_data = array_merge($arr_data,$formdata_host);
//array_push($arr_data,$formdata_host);

//Reindex the Array
//$arr_data = array_values($arr_data);

This will put the server key on the top level of the array where you want it.

Output

{
    "server": {
        "TESTSERVER": [
            {
                "svc": "TESTSERVER",
                "id": 1
            }
        ]
    }
}

Sandbox

I could tell array_values was wrong because you want server to be a "top-level" key, which can never happen with array_values because it would strip that key.

And array_push is rarely what you want to do when combining arrays, because it pushes the whole structure of the array on. So you have

 [ "server" => ..... ]

Being added, instead of just

"server" => .....

The contents.

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
  • Many Thanks.. This Answer is very close to the Solution I need. But one Problem i still have. How can i push an additional Server to the JSON-File? With every new TESTSERVER the List is deleted completely. How can i add a different new Server; so TESTSERVER1 and TESTSERVER2 in one JSON-File? – danielx78 Sep 17 '18 at 16:04
  • So i have to combine array_merge with array_push? Am i right, that i have to change the Structure of the JSON-File from { "server" => .. } to [ "server" => ... ]? Is there another possibility to add a new Server to the File? – danielx78 Sep 17 '18 at 16:34
  • `So i have to combine array_merge with array_push` -- No. Use array_merge instead of array_push and remove array_values. As I posted. – ArtisticPhoenix Sep 17 '18 at 17:14
  • `Am i right, that i have to change the Structure` No, if you read what I posted, which shows the structure you want and can get. If again, you read what I posted. – ArtisticPhoenix Sep 17 '18 at 17:15
  • You have misunderstood me.. I have read your answer very well. Your Solution is working fine. I have replaced array_push with array_merge und removed array_values from the code. My Problem is, that i want to add new Values instead of replace the whole JSON-File. So in the End i want to add a second Server -> TESTSERVER2 to the JSON-File. – danielx78 Sep 17 '18 at 19:10
0

I have found the Solution:

array_replace_recursive($array, $array2);

Source: php array_merge without erasing values?

danielx78
  • 41
  • 4