How do I edit and delete the data in a json file?

3

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

    <?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>
    
asked by anonymous 12.10.2016 / 04:11

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 );
    
12.10.2016 / 04:37