Created
May 19, 2020 04:45
-
-
Save rwaddin/71441af29c521cab51d8daa7603a5011 to your computer and use it in GitHub Desktop.
Delete data file json with php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // File json yang akan dibaca | |
| $file = "anggota.json"; | |
| // Mendapatkan file json | |
| $anggota = file_get_contents($file); | |
| // Mendecode anggota.json | |
| $data = json_decode($anggota, true); | |
| // Membaca data array menggunakan foreach | |
| foreach ($data as $key => $d) { | |
| // Hapus data kedua | |
| if ($d['no'] === 2) { | |
| // Menghapus data array sesuai dengan index | |
| // Menggantinya dengan elemen baru | |
| array_splice($data, $key, 1); | |
| } | |
| } | |
| // Mengencode data menjadi json | |
| $jsonfile = json_encode($data, JSON_PRETTY_PRINT); | |
| // Menyimpan data ke dalam anggota.json | |
| $anggota = file_put_contents($file, $jsonfile); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment