In PHP programming, the use of arrays is very common, and JSON is also widely used as a lightweight data exchange format. When using PHP to process front-end data, it is often necessary to convert the array in PHP into JSON format and send it to the front-end for processing. And when we use PHP to index an array, we need to convert it into JSON format for easy delivery and parsing.
Before you start, make sure your php version is 5.2 or above, because the json_encode function is new in this version.
First, we need to create an index array. The following code creates an index array named $books, which contains three elements. Each element contains two attributes: book title and author.
$books = array(
array('title' => 'PHP Introduction Tutorial', 'author' => 'Zhang San'),
array('title' => ; 'JavaScript practice', 'author' => '李思'),
array('title' => 'Python basic tutorial', 'author' => '王五')
) ;
Next, we need to convert this array into JSON format. PHP provides a very convenient function json_encode(), which can convert a PHP variable into JSON format.
The following code demonstrates how to convert the $books array to JSON format:
$json = json_encode($books);
In this code, the json_encode function will $ Convert the books array into a JSON string and assign the result to the variable $json.
In this example, the value of $json is:
[{"title":"PHP Introduction Tutorial","author":"Zhang San"},{"title": "JavaScript Practical Combat","author":"李思"},{"title":"Python Basic Tutorial","author":"王五"}]
Next, we need to convert the JSON formatted string back to a PHP array. PHP provides a function json_decode(), which can convert JSON format strings into PHP variables (usually arrays or objects).
The following code demonstrates how to convert $json back to a PHP array:
$decoded = json_decode($json, true);
In this example, the json_decode function Decode the $json string into a PHP array and assign the result to the $decoded variable. The second parameter (true) tells the function to convert the JSON object to a PHP associative array, not a PHP object. This will allow us to access the elements of the array using $decoded['title'] and $decoded['author'].
The following is a complete sample code, including array creation, JSON encoding and JSON decoding:
// Create An index array
$books = array(
array('title' => 'PHP Getting Started Tutorial', 'author' => 'Zhang San'),
array('title' => ; 'JavaScript practice', 'author' => '李思'),
array('title' => 'Python basic tutorial', 'author' => '王五')
) ;
// Convert array to JSON
$json = json_encode($books);
// Output JSON
echo $json;
/ / Convert JSON back to array
$decoded = json_decode($json, true);
// Output the contents of the array
foreach ($decoded as $book) {
echo $book ['title'] . ' - ' . $book['author'] . '
';
}
Run the above code and the output will be as follows:
[{ "title":"PHP Getting Started Tutorial","author":"Zhang San"},{"title":"JavaScript Practical Combat","author":"李思"},{"title":"Python Basics Tutorial" ,"author":"王五"}]
PHP introductory tutorial-Zhang San
JavaScript practice-Li Si
Basic Python tutorial-Wang Wu
Summary
It is very convenient to use the json_encode and json_decode functions in PHP to convert index arrays to JSON format and back. When we need to send data to or receive front-end data, converting the data into JSON format can greatly simplify the data transmission and parsing process. Hope this article can help you understand how to convert index array into JSON format.
The above is the detailed content of How to convert php index array to json. For more information, please follow other related articles on the PHP Chinese website!