NinjaShawarma NinjaShawarma - 29 days ago 8
JSON Question

How to get all the JSON records saved instead of the last record only

Here is my code, I'm trying to get all the records saved in the JSON file, but I end up with the last record only. Thanks!

try {
$output = $web_service->query($input);
if($output['HTTP_OK'] == true) {
foreach (json_decode($output['DATA']) as $obj) {
print_r($obj);
}
}
}

file_put_contents("data.json", json_encode($obj));

Answer

This solved my problem

try {
    $output = $web_service->query($input);
    if($output['HTTP_OK'] == true) {
        print_r($output['DATA']);
        foreach (json_decode($output['DATA']) as $obj) {
            print_r($obj);
        }

    }

file_put_contents("data.json", $output['DATA']);