I try to write to JSON using php. The first time is written and you can use the file, and the second time returns null when decoding due to the wrong json file format, here is the code that writes
$name = clean($_POST['title']); $message = clean($_POST['message']); $new_post = array("title" => $name, "text" => $message); $new_post = json_encode($new_post); file_put_contents('data.json', $new_post, FILE_APPEND); echo "Success"; function clean($value = ""){ $value = htmlspecialchars(strip_tags(stripcslashes(trim($value)))); return $value; } And he writes in this form {"title": "value1", "text": "value2"} {"title": "value3", "text": "value4"} .... Writes in one line, does not transfer the caret below. Because of this, after the second write to this file, it cannot be used. Here is the code that reads:
<?php $title = clean($_GET['query']); function clean($value = ""){ $value = htmlspecialchars(strip_tags(stripcslashes(trim($value)))); return $value; } $result_string = search($title); function search($title = ""){ $ret_str = ""; $text = file_get_contents('../data.json'); $obj = json_decode($text, true); echo in_array($title, $obj); foreach ($obj as $key=>$value){ if($title == $value){ $ret_str = $obj['text']; }else{ echo "WTF?!"; } } return $ret_str; } echo "<br>".$result_string;