使用php从ajax发送的数据写入带有数组的json文件

I am sending a string and an array to a php file from an AJAX request

My html looks like this

<form>
  <input class="description" type="text">
  <input type="submit">
</form>

My js file looks like this

$('form').submit(function(e){

    e.preventDefault();

    var description = $('.description').val();

    var fileNames = ['1.jpg', '2.jpg'];

    var data = {
        description,
        fileNames
    };

    $.ajax({
        url: 'details.php',
        type: 'POST',
        data,
        success: function(data){
            console.log(data)
        }
    });
});

My php file looks like this

<?php

$str = file_get_contents("test.json");

// // decode JSON
$json = json_decode($str, true);

$decodedJSON =  json_decode($str, true);
var_dump($decodedJSON);

$description = $_REQUEST;

$milliseconds = round(microtime(true) * 1000);

$description->time = $milliseconds;

file_put_contents('test.json', json_encode($description, JSON_PRETTY_PRINT));

?>

This sets the json file as

{"description":"test text entered","SQLiteManager_currentLangue":"2"}

I want the json file to look like where the number is the current time in ms.

{
    "1495134004244": {
        "images": [
            "2.JPG"
        ],
        "description": "test"
    }
}

You need to redo pretty much everything. I would do it in this manner:

HTML:

<form>
    <input type="text" name="description">
    <input type="hidden" name="images[]" value="1.jpg">
    <input type="hidden" name="images[]" value="2.jpg">
    <input type="submit">
</form>

JS:

$('form').submit(function(e){
    e.preventDefault();
    $.ajax({
        url: 'details.php',
        type: 'POST',
        data: $(this).serialize(),
        success: function(data){
            console.log(data)
        }
    });
});

PHP:

// get data from request
$newArray = $_REQUEST;

// get json from file
$json = file_get_contents('test.json');

// turn json into array
$masterArr = json_decode($json, true);

// get current time in milliseconds
$milliseconds = round(microtime(true) * 1000);

// use milliseconds as array key and use the new array as its value
$masterArr["$milliseconds"] = $newArray;

// turn array back to json
$json = json_encode($masterArr, JSON_PRETTY_PRINT);

// save json to file
file_put_contents('test.json', $json);

// echo the json so that you can use it in the AJAX call
echo $json;