如何在JSON文件中追加新元素而不在PHP中获取此文件的所有数据

如何在JSON文件中追加新元素而不在PHP中获取此文件的所有数据

问题描述:

i need to append some elements to json file this is my code:

routes.php (stage1)

$array = ['id' => '1', 'number' => '123123', 'name' => 'ahmed'];
$json_array = json_encode($array);
Storage::append('test.json', json_encode(['user_name' => 'ahmedsk']));

test.json

{"user_name":"ahmedsk"}
{"id":"1","number":"24142","username":"ahmedsk"}

but when i make print_r() for $result it's not feedback

routes.php (stage2)

$result = Storage::get('test.json');
print_r($result);

With knowing i don't need to use the method of get all data from JSON file and edited it and resave it in file.

Thanks

The question is some how vague.
if you need to print the json file as an array this is what you need :

routes.php (stage2)

$result = Storage::get('test.json');
print_r(json_decode("[" . preg_replace("/
+/",",",$result) . "]"));

test : https://eval.in/499591