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.
change json key name [ json generated using json_encode ]
Only if you rewrite 'm yourself. You could use:
$rewriteKeys = array('a' => 'foo', 'b' => 'something', 'c' => 'bar', 'd' => 'foo', 'e' => 'baz');
$newArr = array();
foreach($arr as $key => $value) {
$newArr[ $rewriteKeys[ $key ] ] = $value;
}
echo json_encode($newArr);
Not sure if that's what you were aiming for.
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;
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/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);
PHP - How to change json values according to another json
//change to arrays
$data = json_decode(YOUR_DATA_JSON, true); // I don't know your json variable names, so replace them
$diagnosticKeys = json_decode(YOUR_DIAGNOSTIC_KEYS_JSON, true);
//iterate over data, this is the one you want to change
foreach ($data as &$dataItem) {//(& is for replacing the values)
//another foreach for diagnostic keys
foreach ($diagnosticKeys as $diagnosticKey) {
if ($dataItem["DIAGNOSTIC_TYPE_ID"] == $diagnosticKey["ID"] {
$dataItem["DIAGNOSTIC_TYPE_ID"] = $diagnosticKey["type"];
}
}
}
//change to json again
$data = json_encode($data);
Not tested but should work.
Related Topics
Group Array Data on One Column and Sum Data from Another Column
How to Get Max and Min Value in a Multidimensional Array
Laravel: Display Difference Between Two Dates in Blade
How to Apply Bindvalue Method in Limit Clause
Cross-Origin Request Headers(Cors) With PHP Headers
Codeigniter - Show Information in Modal Window
Getting Date Format M-D-Y H:I:S.U from Milliseconds
Reference - What Does This Error Mean in PHP
Are PHP Short Tags Acceptable to Use
Session Id Is Changing After Every Request
Send File Via Curl from Form Post in PHP
Get Woocommerce Carts Total Amount
How to Calculate the Difference Between Two Dates Using PHP
How to Squeeze Error Message Out of Pdo