To convert a PHP array to JSON data string, you can use the json_encode() function. The json_encode() function converts passed PHP objects into JSON formatted strings. You can control the flow of the conversion by passing encoding parameters to the json_encode() function. In this PHP Array to JSON example, we use the json_encode() function to convert a PHP Array to a JSON string. Click Execute to run the Convert PHP Array to JSON String Example online and see the result.
How to encode a PHP array into a JSON string?
There are two types of arrays in PHP: regular and associative (an associative array uses a key-value structure to store data). The json_encode() function can convert both array types to a JSON data string. We convert an associative array to a JSON string in the example below.