logicK
logicK

Reputation: 207

.JSON file creates NULL value via PHP

So I'm trying to figure out why I get a NULL value in my .json file after I try to write the array to it. It only happens with the 'array_push' line after creating a new file. If there is a .json file already there with a value in it, it'll write to it correctly. The only thing I could guess was the file is missing the '{' and '}' in it upon creation.

I've got a small work around so far, but not sure that this is the right way to do it. Can someone tell me if this is good or bad?

Just to clarify, the .json document only holds the vault of NULL, there are no array elements or anything in the file besides the word NULL.

//CHECK IF FILE EXISTS, ELSE CREATE IT
$log_filename = "./site_files/logs/error-404-log.json";
if(!file_exists($log_filename)) {
    touch($log_filename);
    //LINE BELOW IS MY WORK AROUND, I'M NOT SURE IF THIS IS THE RIGHT WAY
    file_put_contents($log_filename, json_encode(json_decode("{}")));
    echo "$log_filename was created. <br />";
}

$log_array = array();
$new_data =  array(
    'current_date_stamp' => $current_date_stamp,
    'current_page_trail' => $current_page_trail
);

$json_data = file_get_contents($log_filename);
if($log_array != "") { $log_array = json_decode($json_data, true); }
//WHEN CREATING A NEW FILE, ARRAY_PUSH GIVES ERROR
array_push($log_array, $new_data);
$json_data = json_encode($log_array, JSON_PRETTY_PRINT);

file_put_contents($log_filename, $json_data);

Upvotes: 0

Views: 315

Answers (2)

mickmackusa
mickmackusa

Reputation: 47894

$log_filename = "error-404-log.json";                // establish the path/to/filename.json
if (file_exists($log_filename)) {                    // if path/to/filename.json exists
    $json = file_get_contents($log_filename);        // access PRETTY json string
    echo "pre-existing data: <div><pre>$json</pre></div><br>";  // display json string
    $array = json_decode($json, true);               // decode to prepare for new data
}

// data always maintains the same structure: an array of 2-element arrays
$array[] = [
    'current_date_stamp' => date('Y-m-d H:i:s'),
    'current_page_trail' => "foo"
];

// create/update the file
$new_json = json_encode($array, JSON_PRETTY_PRINT);  // re-encode updated array data
echo "new data: <div><pre>$new_json</pre></div>";    // display new json string
file_put_contents($log_filename, $new_json);         // create or overwrite file

Upvotes: 1

Sven Hakvoort
Sven Hakvoort

Reputation: 3621

Your code is failing because you are comparing $log_array with an empty string, this condition will always pass because this array will never be an empty string. In the if statement you decode the contents of the file, when the file has no content this will be an empty string and it will return NULL, after this you write this NULL value to your file. If you check if $json_data is an empty string your code should work, I would also recommend doing it like this:

$log_array = array();
$new_data =  array(
 'current_date_stamp' => $current_date_stamp,
 'current_page_trail' => $current_page_trail
);

$json_data = file_get_contents($log_filename);
if($json_data != "") { 
    $log_array = json_decode($json_data, true); 
    //WHEN CREATING A NEW FILE, ARRAY_PUSH GIVES ERROR
    array_push($log_array, $new_data);
    $json_data = json_encode($log_array, JSON_PRETTY_PRINT);

    file_put_contents($log_filename, $json_data);
}

This is better since if the string is empty all other actions are irrelevant.

Upvotes: 0

Related Questions