Read and write json in php

Read json-file:

$data = file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/myfile.json");
$myArray = json_decode($data, true);

Write array to a json-file:

$jsonArray = json_encode($myArray);
file_put_contents("/myfile.json", $jsonArray);

To return json when php-file is called, simply:

echo json_encode($myArray); 

Published
Categorized as php