user2883088
user2883088

Reputation: 95

How to Overwrite Json if Fields Match - PHP

Working on a website and need to store data for each user. Currently using json files and the way it is set up currently it overwrites the data each time.

1st question, is using one json file the best way to house this data or should I set up a directory for each user?

2nd question, if one file is the best way to go, how do I append 'unique' data? I found some example code from the posts on "Overwrite JSON if fields match PHP" but it is not working for me. It is not writing to the file at all now.

Original code:

$posts[] = array('vhclID'=> $vhclID, 'statVal1'=> $engStat, 'statVal2'=> $brakeStat);

$response['posts'] = $posts;

$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);

Revised code to be able to append new data and eliminate redundancies(Does not work):

$file = file_get_contents('results.json');
$data = json_decode($file);
unset($file);//prevent memory leaks for large json.
//insert data here
$data[vhclID] = array('vhclID'=> $vhclID, 'statVal1'=> $engStat, 
'statVal2'=> $brakeStat);
//save the file
$data = array_values($data);
file_put_contents('results.json',json_encode($data));
echo json_encode($data);
unset($data);//release memory

Thanks for your help!!

Upvotes: 0

Views: 258

Answers (3)

Vince
Vince

Reputation: 1851

I mess around with PHP and json data a lot.

One thing I've noticed is that json_decode will create a PHP object(stdClass) by default

Example

Contents of results.json >>> {"example":"test"}

$file = file_get_contents("results.json");

$json = json_decode($file);

var_dump($json); // Outputs: object(stdClass)#14 (1) { ["example"]=> string(4) "test" }

If you add true as the second parameter to json_decode you end up with an array instead

Example

$file = file_get_contents("results.json");

$json = json_decode($file, TRUE); // Added TRUE as second parameter

var_dump($json); // Outputs: array(1) { ["example"]=> string(4) "test" }

Once you have your appropriate data, you can modify and change the $json however you want and then re-write it to the .json file.

So for question 1: Having an individual json file for each user (eg: userID-001.json, userID-002.json) is probably the better way to go.

For question 2: You can take the individual file, grab the contents and store it in a PHP array using json_decode($data, TRUE) // with true as second parameter if you want an array and then modify the array and resave it (using json_encode).

Hope this helps~!

Upvotes: 0

nike4613
nike4613

Reputation: 39

$fp = fopen('results.json', 'r');
$postjson = json_decode(fread($fp, 1024*1024), true);
fclose($fp);

$posts = ($posts==array()) array('vhclID'=> $vhclID, 'statVal1'=> $engStat, 'statVal2'=> $brakeStat) : $postjson['posts'];

$response['posts'] = $posts;

$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);

Should do what you want. Modify $posts.

Upvotes: 0

graemeboy
graemeboy

Reputation: 610

You should use a database if you're storing typical user data; clearly you don't want to load megabytes of user data just to observer or modify one field for one user.

If you have some posted data, and I understand your question correctly, you might do something like this (but add more security):

$new_data = $_POST[];
foreach ($new_data as $name=>$datum) {
 if (empty($data[vhclID][$name]) {
    // This means that this field is unique
    $data[vhclID][$name] = $datum;
 }
}

And then just save that data to your JSON file.

Upvotes: 1

Related Questions