How to Update/Edit a JSON File Using PHP

How to update/edit a JSON file using PHP

First, you need to decode it :

$jsonString = file_get_contents('jsonFile.json');
$data = json_decode($jsonString, true);

Then change the data :

$data[0]['activity_name'] = "TENNIS";
// or if you want to change all entries with activity_code "1"
foreach ($data as $key => $entry) {
if ($entry['activity_code'] == '1') {
$data[$key]['activity_name'] = "TENNIS";
}
}

Then re-encode it and save it back in the file:

$newJsonString = json_encode($data);
file_put_contents('jsonFile.json', $newJsonString);

How to update/edit a JSON using PHP

You can covert the json string to an array-object using json_encode($,TRUE).

Then you will be able to loop through the keys of the object if you obtain the keys by array_keys().

And then you can either use a separate variable to iterate through the main object properties and change that object, or directly access the main object and change it at the source, which is what I did below:

$data = '{"1":{"TchID":"G303992","TchData":{"TchID":"G303992","TchNama":"G303992","TchPassword":43511824}},
"2":{"TchID":"G141843","TchData":{"TchID":"G141843","TchNama":"G141843","TchPassword":22932450}}}';

$guru = json_decode($data, true);

$keys = array_keys($guru);

foreach ($keys as $key) {
if($guru[$key]['TchID'] == 'G303992'){
$guru[$key]["TchNama"] = "Alex J";
}
}

$viewchange = json_encode($guru);
echo $viewchange;

How to edit specific JSON key values using PHP

You can try this.

Firstly, decode your JSON:

$json_object = file_get_contents('some_file_name.json');
$data = json_decode($json_object, true);

Then edit what you want such as:

$data['some_key'] = "some_value";

Finally rewrite it back on the file (or a newer one):

$json_object = json_encode($data);
file_put_contents('some_file_name.json', $json_object);

Note: I assumed that the JSON comes from a file, but in place of that file system function you can very well use anything that returns a JSON object.

Update value JSON file using php

You have a double equals in your if statement. Change it to one equals to assign a value.

$data[$key]['url'] == $url;

to

$data[$key]['url'] = $url;

UPDATE

Based on following pastes and comments, it's the form POST which isn't sending, because the form elements only have an id and not a name.

how to update json file with .php?

Here's a simple example...

<?php

// Define the file here
define('JSON_FILE', '/tmp/data.json');

// Create the empty file
if(!file_exists(JSON_FILE)) {
$int_bytes = file_put_contents(JSON_FILE, json_encode((object)[
'events' => ['First entry']
]));
echo "Wrote {$int_bytes} bytes to new file", PHP_EOL;
}

// Load and decode
$obj_data = json_decode(file_get_contents(JSON_FILE));

// Show the data after loading
print_r($obj_data);

// Set some data
$obj_data->awesome = true;
$obj_data->name = "tom";

// Add an event to the array
$obj_data->events[] = "Event at " . time();

// Show the data before saving
print_r($obj_data);

// Encode and save!
$int_bytes = file_put_contents(JSON_FILE, json_encode($obj_data));

echo "Wrote {$int_bytes} bytes", PHP_EOL;

Updating JSON file through PHP

You want to pass $e by reference in your foreach:

<?php
// this originally comes from file
$json = <<<EOT
[
{
"id": 3,
"title": "SOME MODS",
"date": "Aug\/Sept 2017",
"done": "yes",
"files changed": {
"1": "index.html",
"2": "style.css"
},
"backend changes\/additions": {
"1": "added some stuff"
},
"additions": {
"1": "logo.jpg"
}
}
]
EOT;

$temp = json_decode($json, true);
$newObj=3; // this is only here for testing

// here's the trick: the & before $e
foreach( $temp as &$e ){
if( $e['id'] == $newObj ) {
if ( $e['done'] === 'no' ){
$e['done'] = 'yes';
} else {
$e['done'] = 'no';
}
}
}
$final_data = json_encode($temp, JSON_PRETTY_PRINT);
file_put_contents('data.json', $final_data);

This way you don't need another array and can save $temp back to your json file.

Short explanation (cracks, please correct me):

in your foreach php makes a copy of $e, so you have to write it back - as you've tried.

When you pass it by reference you work on the original item $e, not a copy. This way you can manipulate it directly.

Update values in 2 different .json files using PHP

I would do something like this:

<?php
//get the index from URL
$index = $_GET['index'];

//get json data
$category_data = file_get_contents('category.json');
$category_data_array = json_decode($data);
$questions_data = file_get_contents('questions.json');
$questions_data_array = json_decode($data);

// get the old category name
$row = $category_data_array[$index];
$old_category = $row->category;
?>

<?php
if(isset($_POST['save'])){
//set the updated values
$input = array(
'category' => $_POST['category'],
);

//update the selected index
$category_data_array[$index] = $input;

// find old category, make new
foreach($questions_data_array as $question){
if ($question->category == $old_category) {
$question->category = $_POST['category'];
break;
}
}

//encode back to json
$category_data = json_encode($category_data_array, JSON_PRETTY_PRINT);
file_put_contents('category.json', $category_data);
$questions_data = json_encode($questions_data_array, JSON_PRETTY_PRINT);
file_put_contents('questions.json', $questions_data);

header('location: category.php');
}
?>

Caveat: I did not test this code. It's like pseudo-code in the shape of actual code.

How to update (overwrite) a json file using php

I think you must need to loop like this to store all details

foreach ($file as $key => $value) {
if((string)$value->id == $this->put('id')) {
$value->name="--";(string)$value->description="--";
}
$new_val[$i] = $value;
$i++;

}


Related Topics



Leave a reply



Submit