Created
October 6, 2016 14:36
-
-
Save tutweb/7f51f22f2ffaa44fd2651aa2288a44c5 to your computer and use it in GitHub Desktop.
Write JSON file with PHP
This file contains 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 | |
// array | |
$array = Array ( | |
"0" => Array ( | |
"id" => "USR1", | |
"name" => "Steve Jobs", | |
"company" => "Apple" | |
), | |
"1" => Array ( | |
"id" => "USR2", | |
"name" => "Bill Gates", | |
"company" => "Microsoft" | |
), | |
"2" => Array ( | |
"id" => "USR3", | |
"name" => "Mark Zuckerberg", | |
"company" => "Facebook" | |
) | |
); | |
// encode array to json | |
$json = json_encode(array('data' => $array)); | |
// write json to file | |
if (file_put_contents("data.json", $json)) | |
echo "File JSON sukses dibuat..."; | |
else | |
echo "Oops! Terjadi error saat membuat file JSON..."; | |
// data.json | |
// {"data":[{"id":"USR1","name":"Steve Jobs","company":"Apple"},{"id":"USR2","name":"Bill Gates","company":"Microsoft"},{"id":"USR3","name":"Mark Zuckerberg","company":"Facebook"}]} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment