NinjaShawarma NinjaShawarma - 1 year ago 60
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 Source

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']);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download