Skip to content

Instantly share code, notes, and snippets.

@rwaddin
Created May 19, 2020 04:44
Show Gist options
  • Save rwaddin/ef68ade4e914d0915634199e773f9548 to your computer and use it in GitHub Desktop.
Save rwaddin/ef68ade4e914d0915634199e773f9548 to your computer and use it in GitHub Desktop.
Insert data to file json with php
<?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);
// Data array baru
$data [] = array(
'no' => 3,
'nama' => 'Elika',
'alamat' => 'Kediri'
);
// 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