How do I edit and delete data from a json file?

Asked

Viewed 1,049 times

3

I am using this code to include and list the data saved in the file "user.json" wanted Saver how do I delete this data and edits

    <?php
        $users = @json_decode(file_get_contents('users.json'), true);

        if(!$users)
        $users = array();
        if (isset($_POST['user'])) {
            $users[] = $_POST['user'];
            $file = fopen('users.json', 'w');
            fwrite($file, json_encode($users));
            fclose($file);
       }
   ?>

   <form action="" method="post">
       <input type="text" placeholder="Nome" name="user[nome]">
       <input type="text" placeholder="Sobrenome" name="user[sobrenome]">
       <input type="text" placeholder="Telefone" name="user[telefone]">

       <input type="submit">
   </form>

   <table border="1" cellpadding="10" cellspacing="0">
       <tr>
           <th>Nome</th>
           <th>Sobrenome</th>
           <th>Telefone</th>
       </tr>

       <?php foreach ($users as $user): ?>
       <tr>
           <td><?= $user['nome'] ?></td>
           <td><?= $user['sobrenome'] ?></td>
           <td><?= $user['telefone'] ?></td>
       </tr>
       <?php endforeach ?>

   </table>
  • You need some parameter to delete?

  • yes would like to delete I managed to list and include but I do not know delete and edit the file

  • You can put in the full code for me to help you?

  • Do you want to delete by which parameter? by name?

  • this is the full code, yes it can be by name same

  • That one reply can help you

  • does not have any way to do with php?

  • if I’m not going to try to use that code it’s very functional

Show 3 more comments

1 answer

0


You can do it this way:

$users  = json_decode($users, true);
foreach ($users  as $key => $value) {
    if (in_array('valor', $value)) {
        unset($users [$key]);
    }
}
$users  = json_encode($users );
  • huum vlew I will use that ;) thank you!

  • If the answer works for you, mark it as accepted

  • blz worked well yes vlew already marked as accepted

Browser other questions tagged

You are not signed in. Login or sign up in order to post.