How to Create a JSON File in PHP?
Teams applying “How to Create a JSON File in PHP?” in a production project can use salary range spread to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the PHP project and test it against the project’s actual database and runtime.
Hi Dev,
This post will give you example of How to Create Json File in php. This post will give you simple example of How to Generate Json File in php?. I would like to show you php Create Json File and Write. I would like to show you How to Save Json File in php?. Alright, let’s dive into the steps.
There ara two ways to create json file in php. in the first example, we will use fopen(), fwrite() and fclose() to create a json file. in the second example, we will use file_put_contents() to create json file in php.Let's see both examples with output.
Example 1:
index.php
<?php
// Create Array for json file
$myArray = [
[ "id" => 1, "name" => "Hardik Savani", "email" => "[email protected]"],
[ "id" => 2, "name" => "Jaydeep Pather", "email" => "[email protected]"],
[ "id" => 3, "name" => "Vivek Pather", "email" => "[email protected]"]
];
// Create new data.json file
$fp = fopen('data.json', 'w');
// Write data on json file
fwrite($fp, json_encode($myArray, JSON_PRETTY_PRINT));
// Close json file
fclose($fp);
print("data.json File created successfully.");
?>
Output:
After run successfully above example, you will see data.json file saved in your root path and file content will be as the below:
[
{
"id": 1,
"name": "Hardik Savani",
"email": "[email protected]"
},
{
"id": 2,
"name": "Jaydeep Pather",
"email": "[email protected]"
},
{
"id": 3,
"name": "Vivek Pather",
"email": "[email protected]"
}
]
Example 2:
index.php
<?php
// Create Array for json file
$myArray = [
[ "id" => 1, "name" => "Hardik Savani", "email" => "[email protected]"],
[ "id" => 2, "name" => "Jaydeep Pather", "email" => "[email protected]"],
[ "id" => 3, "name" => "Vivek Pather", "email" => "[email protected]"]
];
// Create json file from array
$data = json_encode($myArray);
file_put_contents('data.json', $data);
print("data.json File created successfully.");
?>
Output:
After run successfully above example, you will see data.json file saved in your root path and file content will be as the below:
[
{
"id": 1,
"name": "Hardik Savani",
"email": "[email protected]"
},
{
"id": 2,
"name": "Jaydeep Pather",
"email": "[email protected]"
},
{
"id": 3,
"name": "Vivek Pather",
"email": "[email protected]"
}
]
I hope it could help you...
- How To Use Php Conditional Statement Example
- How to Check if String Contains Regex in PHP?
- PHP Array_column() Function Example
- PHP Generate List of Random Numbers Between 0 and 100 Example
- How to User Login with Facebook in PHP?
- How to Explode Every Character into Array Using PHP Example?
- PHP Form Validation Tutorial
- PHP Convert XML to JSON Example