PHP is a widely used server-side scripting language commonly used in website development, dynamic web page generation, and web application development. In the development of many applications, data transmission is a very important part, and JSON is a lightweight data exchange format that is widely used because of its simplicity, ease of use, and scalability. In PHP, we can use built-in functions to convert arrays into JSON format.
json_encode() function is the most basic method in PHP to convert an array into JSON format. As a built-in function, its use is very simple. You only need to pass in the array to be converted as a parameter. For example, we have the following array:
$data = array( 'name' => 'John', 'age' => 20, 'gender' => 'male' );
We can use the following code to convert the array into JSON format:
$json_data = json_encode($data);
After conversion, $json_data will be a JSON string like this :
{ "name": "John", "age": 20, "gender": "male" }
It should be noted that the json_encode() function will escape non-ASCII characters into Unicode encoding by default. If you need to generate an ASCII-encoded JSON string, you need to add the JSON_UNESCAPED_UNICODE parameter to the function.
$json_data = json_encode($data, JSON_UNESCAPED_UNICODE);
In some cases, we need to obtain JSON data through cross-domain methods, and JSONP is a method that allows cross-domain The requested JSON data format. In PHP, we can use the json_encode() function to generate JSON data that supports JSONP. For JSON data that supports JSONP, the callback function name needs to be passed in as the second parameter of the json_encode() function. For example:
$data = array( 'name' => 'John', 'age' => 20, 'gender' => 'male' ); $callback = $_GET['callback']; $json_data = json_encode($data); echo $callback . '(' . $json_data . ')';
This code will generate data in the following format:
callback_name({ "name": "John", "age": 20, "gender": "male" })
When we request the JSON data using cross-domain methods, the callback function name will be called in the response data.
json_decode() function is a function that converts JSON format data into a PHP array. Unlike the json_encode() function, json_decode() requires two parameters: a JSON string and an optional parameter $assoc. When $assoc is true, the function will convert the JSON string into a PHP associative array, otherwise it will be converted into a PHP object. For example:
$json_data = '{ "name": "John", "age": 20, "gender": "male" }'; $data = json_decode($json_data, true); var_dump($data);
After conversion, $data will be an array in the following format:
array(3) { ["name"]=> string(4) "John" ["age"]=> int(20) ["gender"]=> string(4) "male" }
It should be noted that if the JSON string does not conform to the JSON format or is encoded incorrectly, json_decode( ) function will return null value.
Finally, it should be pointed out that although the json_encode() and json_decode() functions provide convenient methods to convert arrays and JSON format data in PHP, you also need to pay attention to data security issues. During data transmission, data needs to be securely filtered and verified to prevent malicious attacks and data leaks.
The above is the detailed content of How to convert array into json data in php. For more information, please follow other related articles on the PHP Chinese website!