John Davidson

Can't Delete Specific Keys and Values from JSON in PHP

0 comments
Message:


I'm trying to delete specific key and value pair from external JSON file in PHP when user clicked delete button on the page.


I'm not familiar with PHP so I couldn't able to figure out what I'm doing wrong. I looked at more than 20 questions on stackoverflow but none of them helped to solve my problem.


What I'm trying to achieve in general to write a simple script which user can write something in textbox and submit, show it on the page and when user wants delete it. (I have done the submit and showing part but stuck on deleting part)


Firstly my JSON file:


{
"departman1":
[
{"departman1stage":"John"},
{"departman1stage":"Test"},
{"departman1stage":"Test2"}
]
}

Part of my index.php file:


    ...
include ('yazdir.php');
include ('sil.php');
<div class='icerik'>


<?php

foreach ($json['departman1'] AS $d){
echo '<p class="' . $d['departman1stage'] .'">';
echo "--> ";
echo $d['departman1stage'];
echo '<form action="sil.php" method="POST"><button type="submit" class="dugme" name="' . $d['departman1stage'] .'"><i class="fa fa-trash"></i></button></form>';
echo "<br>";
echo "</p>";
}
?>
</div>
<br>
<form action="yazdir.php" method="POST">
<div class="icerik"><p><input type="text" name="departman1stage"></p></div>
<br>
<input type="submit" value="Gonder">
</form>
...

It is not related with problem but I still put my yazdir.php code:


$json_sifreli = file_get_contents ("data.json");
$json = json_decode($json_sifreli, true);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$data = array(
'departman1stage' => $_POST["departman1stage"]
);
array_push ( $json['departman1'], $data );
$json_sifreleme = json_encode($json, true);
file_put_contents('data.json', $json_sifreleme);
}

sil.php is currently blank, as I said I tried different methods. Some of them deleted whole JSON file and put 'null' into it. Some of them deleted unrelated key and value pair. As a last resort, I decided to write here.


Thanks in advance, sorry for taking your time.



My Answer:
To delete a specific key and value pair from your JSON file in PHP, you can modify your `sil.php` file as follows:

php
$json_sifreli = file_get_contents("data.json");
$json = json_decode($json_sifreli, true);

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$keyToDelete = $_POST["keyToDelete"];

foreach ($json['departman1'] as $key => $value) {
if ($value['departman1stage'] == $keyToDelete) {
unset($json['departman1'][$key]);
}
}

$json_sifreleme = json_encode($json, true);
file_put_contents('data.json', $json_sifreleme);
}


In your `index.php` file, you need to modify the form action to point to `sil.php` and pass the key to delete as a POST parameter:

php
echo '
';
echo '';
echo '';
echo '
';


With these changes, when the user clicks the delete button, the corresponding key and value pair will be removed from the JSON file.

Rate this post

3 of 5 based on 2541 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us