JSON is one of the popular data formats across all technologies. JSON handling with PHP is easier than it looks. Most of the APIs uses JSON format for data interchange. For example, if you want to extract profile data from Facebook using its API, it returns the data in JSON format. There is no option khổng lồ ignore JSON.

The JSON object contains an associative array of “name:value” pairs whereas the JSON array contains a sequence of values with default numeric indexes.Bạn đã xem: Json php
The JSON is evolved from the JavaScript và ECMAScript programming language. Standard ECMA-404 contains the specification for a valid JSON syntax.Bạn vẫn xem: How lớn parse json from string with data have special chars
Example of a JSON Object & Array
In the JSON introduction, we saw that the format can be varied based on the data structure. Let us see the examples for these two JSON formats object & array.
Bạn đang xem: Convert json php : how to encode, write, parse, decode and convert
Example of a valid JSON Object
JSON object format consists of a collection of key-value pairs of data. Below structure shows the example of a valid JSON object structure.
"FIFA_World_Cup_Winners" : "2018" : "France", "2014" : "Germany", "2010" : "Spain", "2006" : "Italy", "2002" : "Brazil"
Example of a JSON Array
In a JSON array, the sequence of data will be listed in an ordered list. There will be no key-value mapping. The following example shows a JSON array structure."FIFA_Last_Five_Winners" : In the above example JSON, the last five winner countries are listed without explicit indexes. So it will be treated as an array of ordered menu of items.
JSON array will be enclosed with whereas the JSON objects are enclosed with curly brackets
JSON Handling with PHP by Parsing file Data
With the reference about the possible JSON structure, now we are going khổng lồ see about JSON handling with PHP khổng lồ parse data from an đầu vào file containing JSON data as shown below.
"FIFA_World_Cup_finals":Pass this tệp tin path lớn the PHP file_get_contents() function và store the JSON data into a variable.
As the input đầu vào JSON tệp tin contains multi-level hierarchical structural data, we have khổng lồ recursively iterate the JSON object using PHP RecursiveArrayIterator.
PHP’s RecursiveArrayIterator simplifies the JSON parsing implementation with few lines of code as shown below. You should realize và make use of the power nguồn of PHP built-in functions.
$val) if(!is_array($val)) if($key == "Year") print ""; print $key." : ".$val . ""; ?>Then, this PHP JSON parsing program will return the following output đầu ra to the browser.

How khổng lồ Decode JSON lớn Array
In a previous tutorial, we have seen how khổng lồ encode-decode JSON using PHP. The json_encode & json_decode PHP functions are used to lớn perform the encode và decode operations respectively.
The following code block shows the syntax of the json_decode function. This function accepts JSON string input as its first parameter. Also, it accepts an optional boolean value as its second parameter.
Consider the following input JSON data. Let us see how it is decoded into an array using json_decode() function.
";print_r($outputArray);?>The output array is,

Decode JSON to Object
By default, the PHP json_decode function will convert the JSON data into an object. The $assoc parameter of the json_decode function will force the output format based on the boolean value passed lớn it.
";print_r($outputObject);?>
JSON khổng lồ Object Conversion Output

Convert PHP Array to JSON
json_encode ( mixed $value > ) : stringThis function accepts the JSON encoded input đầu vào as its first parameter. It should mandatorily be specified with a JSON string while invoking json_encode lớn process the data conversion.
Xem thêm: Mang Thai Bao Nhiêu Tuần Thì Máy? Thai Bao Nhiêu Tuần Thì Máy
Below PHP script is used khổng lồ convert a PHP array into a JSON format. In this example, I have used an associative array which will be sent to the json_encode function.
array ( "Year" => "2018", "data" => array ( "Winner" => "France", "Score" => "4-2", "Runner-up" => "Croatia") ) );$encodedJSON = json_encode($inputArray, JSON_PRETTY_PRINT);print($encodedJSON);?>By running this program, the encoded JSON data will be printed on the browser as shown below.
"FIFA_Last_World_Cup_final": "Year": "2018", "data": "Winner": "France", "Score": "4-2", "Runner-up": "Croatia" Note:
PHP JSON encode decode functions will work with UTF-8 formatted strings.Read JSON via AJAX & Process
In this section, we are going to lớn see about JSON handling with PHP & AJAX. I have used JavaScript khổng lồ send the AJAX điện thoại tư vấn to the PHP via an XML HTTP request.
Add the below script on the HTML page from where you want khổng lồ access JSON data returned by PHP.
The AJAX callback will get the response JSON from the PHP endpoint. In this example, the PHP endpoint is specified as the JavaScript AJAX URL by setting the kích hoạt parameter in the URL query string. It calls the getJSON.php file via AJAX.
The returned JSON đầu ra is received in the onreadystatechange callback. This output data is parsed with the JSON.parse method of the JavaScript.
In the PHP script, the JSON data is returned lớn the AJAX by using PHP print statement. Before creating the đầu ra JSON data, it checks the required action parameter sent via AJAX is not empty.
array ( "Year" => "2018", "data" => array ( "Winner" => "France", "Score" => "4-2", "Runner" => "Croatia") ) );$encodedJSON = json_encode($inputArray, JSON_PRETTY_PRINT);print($encodedJSON);exit;}?>This AJAX script process the JSON response in the success callback function. In this callback, the read JSON data are used to update the UI. The output đầu ra will be displayed as below.

How khổng lồ Access a JSON Feed và Display Data
Accessing JSON feed URL can be done in various ways. In the above example on parsing JSON tệp tin data via PHP, we have used file_get_contents() function.
The file_get_contents() will not work on the hệ thống because of the security directives enable with the PHP.ini configurations. So, we can also use CURL script to lớn extract JSON data from the remote feed URL.
The following example will show how lớn use PHP CURL to access JSON feed và display data to lớn the browser.
";print_r($outputArray);exit;?>
How lớn Convert JSON lớn JavaScript Object
We have already seen how to convert JSON to lớn JavaScript Object while creating the example on JSON handling with PHP via AJAX without jQuery.In this section, we are going to see how to convert JSON data into a JavaScript Object. Also, we have to lớn loop through the resultant Javascript Object array.
JSON to lớn JavaScript Object conversion can be done by using various ways. As lượt thích as the previous example, we can vì chưng this by using JavaScript’s built-in JSON.parse() method.
Using jQuery the $.parseJSON method will tư vấn this conversion và make it working in many obsolete web browsers. The following code uses jQuery lớn convert a JSON input string into a JavaScript Object.