- Creating json file in php
- Generate JSON File in PHP
- Use file_put_contents() Function to Generate a .Json File in PHP
- Related Article — PHP JSON
- Tutorial: Create JSON File From MySQLi in PHP
- Getting Started:
- Creating Database
- Creating the database connection
- Creating The Interface
- PHP - Create JSON File From MySQLi
- Creating the PHP Query
- Creating the Main Function
Creating json file in php
- Different ways to write a PHP code
- How to write comments in PHP ?
- Introduction to Codeignitor (PHP)
- How to echo HTML in PHP ?
- Error handling in PHP
- How to show All Errors in PHP ?
- How to Start and Stop a Timer in PHP ?
- How to create default function parameter in PHP?
- How to check if mod_rewrite is enabled in PHP ?
- Web Scraping in PHP Using Simple HTML DOM Parser
- How to pass form variables from one page to other page in PHP ?
- How to display logged in user information in PHP ?
- How to find out where a function is defined using PHP ?
- How to Get $_POST from multiple check-boxes ?
- How to Secure hash and salt for PHP passwords ?
- Program to Insert new item in array on any position in PHP
- PHP append one array to another
- How to delete an Element From an Array in PHP ?
- How to print all the values of an array in PHP ?
- How to perform Array Delete by Value Not Key in PHP ?
- Removing Array Element and Re-Indexing in PHP
- How to count all array elements in PHP ?
- How to insert an item at the beginning of an array in PHP ?
- PHP Check if two arrays contain same elements
- Merge two arrays keeping original keys in PHP
- PHP program to find the maximum and the minimum in array
- How to check a key exists in an array in PHP ?
- PHP | Second most frequent element in an array
- Sort array of objects by object fields in PHP
- PHP | Sort array of strings in natural and standard orders
- How to pass PHP Variables by reference ?
- How to format Phone Numbers in PHP ?
- How to use php serialize() and unserialize() Function
- Implementing callback in PHP
- PHP | Merging two or more arrays using array_merge()
- PHP program to print an arithmetic progression series using inbuilt functions
- How to prevent SQL Injection in PHP ?
- How to extract the user name from the email ID using PHP ?
- How to count rows in MySQL table in PHP ?
- How to parse a CSV File in PHP ?
- How to generate simple random password from a given string using PHP ?
- How to upload images in MySQL using PHP PDO ?
- How to check foreach Loop Key Value in PHP ?
- How to properly Format a Number With Leading Zeros in PHP ?
- How to get a File Extension in PHP ?
- How to get the current Date and Time in PHP ?
- PHP program to change date format
- How to convert DateTime to String using PHP ?
- How to get Time Difference in Minutes in PHP ?
- Return all dates between two dates in an array in PHP
- Sort an array of dates in PHP
- How to get the time of the last modification of the current page in PHP?
- How to convert a Date into Timestamp using PHP ?
- How to add 24 hours to a unix timestamp in php?
- Sort a multidimensional array by date element in PHP
- Convert timestamp to readable date/time in PHP
- PHP | Number of week days between two dates
- PHP | Converting string to Date and DateTime
- How to get last day of a month from date in PHP ?
- PHP | Change strings in an array to uppercase
- How to convert first character of all the words uppercase using PHP ?
- How to get the last character of a string in PHP ?
- How to convert uppercase string to lowercase using PHP ?
- How to extract Numbers From a String in PHP ?
- How to replace String in PHP ?
- How to Encrypt and Decrypt a PHP String ?
- How to display string values within a table using PHP ?
- How to write Multi-Line Strings in PHP ?
- How to check if a String Contains a Substring in PHP ?
- How to append a string in PHP ?
- How to remove white spaces only beginning/end of a string using PHP ?
- How to Remove Special Character from String in PHP ?
- How to create a string by joining the array elements using PHP ?
- How to prepend a string in PHP ?
Generate JSON File in PHP
In this article, we will introduce the method to generate a .json file in PHP.
Use file_put_contents() Function to Generate a .Json File in PHP
The built-in function file_put_contents() could write the content into a file in PHP. It searches for the file to write in, and if the desired file is not present, it creates a new file. We can use this function to create a .json file. The correct syntax to use this function is as follows
file_get_contents($pathOfFile, $info, $customContext, $mode);
This function returns the number of bytes written on the file if successful and false otherwise.
The below program will create a new .json file and store JSON data into it
php // data strored in array $array = Array ( "0" => Array ( "id" => "01", "name" => "Olivia Mason", "designation" => "System Architect" ), "1" => Array ( "id" => "02", "name" => "Jennifer Laurence", "designation" => "Senior Programmer" ), "2" => Array ( "id" => "03", "name" => "Medona Oliver", "designation" => "Office Manager" ) ); // encode array to json $json = json_encode($array); $bytes = file_put_contents("myfile.json", $json); echo "The number of bytes written are $bytes."; ?>
We use json_encode() function to convert the data stored in the array to a JSON string. Once the data is converted to a JSON string, file_put_contents() function creates a .json file and writes data into it. The output shows the number of bytes, which means the data is written successfully.
The number of bytes written is 207.
Related Article — PHP JSON
Tutorial: Create JSON File From MySQLi in PHP
In this tutorial, we will create a Create JSON File From MySQLi using PHP. This code can generate a JSON file from the MySQLi database when the user clicks the button. The system itself uses the PHP POST method to call a specific function that will extract the MySQLi table into a JSON file. This is a user-friendly kind of program feel free to modify it.
We will be using JSON as data management to store user data, and it will act as database storage. It is an open-standard file format that uses human-readable text to transmit data objects into the local server.
Getting Started:
First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the XAMPP server https://www.apachefriends.org/index.html .
And, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/ .
Creating Database
Open your database web server then create a database name in it db_mysqli_json ; after that, click Import, then locate the database file inside the folder of the application then click ok.
Creating the database connection
Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
Creating The Interface
This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
PHP - Create JSON File From MySQLi
Creating the PHP Query
This code contains the php query of the application. This code will store the data input to MySQLi database server. To make this just copy and write these block of codes below inside the text editor, then save it as save.php.
Creating the Main Function
This code contains the main function of the application. This code will extract the MySQLi data into JSON file. To make this just copy and write these block of codes below inside the text editor, then save it as create.php
There you have it we successfully created Create JSON File From MySQLi using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!