Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Write JSON file with PHP
<?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
You can’t perform that action at this time.