Answer
Solution:
Here is some simplified code, take your stringified json, turn it into a php object, and then join the array and dump it to a text file.
<?php
if(isset($_POST['news'])){
$submission = $_POST['searcharray'];
$json = json_encode($submission);
if($json !== false) {
$json_ob = json_decode($submission);
file_put_contents('data.txt', implode('&', $json_ob->searcharray));
}
}
?>
<html>
<head>
<script>
searcharray = ["yemen", "america", "franc", "germany"];
const json = JSON.stringify({searcharray});
const formData = new FormData();
formData.append('searcharray', json);
formData.append('news', 'true');
var request = new XMLHttpRequest();
request.open("POST", "");
request.send(formData);
</script>
</head>
<body>
<body>
</html>
Output of the data.txt after submission:
yemen&america&franc&germany
The POST submission looks like this:
{"searcharray":["yemen","america","franc","germany"]}
The first value could be accessed thus:
$json_ob->searcharray[0]
You could de-key the JSON object, just turn it into a list:
JSON.stringify(searcharray);