How to Make/Create JSON File Using PHP Array In Laravel - PHP; In this post, you will learn how to create a JSON file using file_put_contents() function.
I have some static data in the PHP array. That I want to store in my JSON file. First, I will encode the data to JSON using json_encode() function and then I used it as second argument in this function JSON_PRETTY_PRINT to create pretty content in the JSON file.
Just copy this code and run, It will create a JSON file in your root path of the project.
$data = json_encode([
[
'name' => 'haresh',
'email' => 'haresh@gmail.com',
'age' => 23
],
[
'name' => 'dharmesh',
'email' => 'dharmesh@gmail.com',
'age' => 25
],
[
'name' => 'paresh',
'email' => 'paresh@gmail.com',
'age' => 21
],
],JSON_PRETTY_PRINT);
file_put_contents('myjsonfile.json',$data);
myjsonfile.json
This is our output of the JSON file which we created.
[
{
"name": "haresh",
"email": "haresh@gmail.com",
"age": 23
},
{
"name": "dharmesh",
"email": "dharmesh@gmail.com",
"age": 25
},
{
"name": "paresh",
"email": "paresh@gmail.com",
"age": 21
}
]
We always thanks to you for reading our blogs.
Dharmesh Chauhan
(Swapinfoway Founder)Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us
Haresh Chauhan
(Co-Founder)We Are Also Recommending You :
- PhonePe Payment Gateway Example PHP
- Trying to access array offset on value of type int
- Cashfree Payment Gateway Integration In Laravel
- How to hide image URL from IMG tag src laravel
- [Fixed] 429: Too Many Attempts When Call API In Laravel 8 9
- Default index.php Root Path Change And Remove Public From The Url
- Laravel 10 Http Guzzle Request Crud Example
- Laravel 9 Create Custom Auth Guard Multi Login Authentication System Tutorial
- Laravel 10 CRUD Operation With YouTube Video Tutorial
- Filtering data excel export in laravel example