Convert array to json php

Everyday use of JSON is to read data from a web server and display the data on a web page. When exchanging data between the browser and a server, data can only be in the form of text.

JSON is a text format, and we can convert any JavaScript object into a JSON format and send that JSON to a server.

PHP has some built-in functions to handle JSON.

To convert PHP array to JSON, use the json_encode[] function. The json_encode[] is a built-in PHP function that converts an array to json. The json_encode[] function returns the string containing a JSON equivalent of the value passed to it, as demonstrated by the numerically indexed array. We can also convert any JSON received from a server into JavaScript objects.

Objects and arrays can be converted into JSON using the json_encode[] method.

Let’s see the following example.

Chủ Đề