Home  >  Article  >  Backend Development  >  Convert php to json

Convert php to json

PHP中文网
PHP中文网Original
2023-05-05 12:44:20853browse

PHP is a commonly used server-side programming language, mainly used to build dynamic websites. When interacting with the front-end data, it is often necessary to convert PHP variables into JSON format to facilitate parsing and use by the front-end. This article will introduce how to convert data into JSON format in PHP.

First of all, we need to ensure that the PHP version >=5.2.0, because after this version, PHP has a built-in json extension for converting data in json format. Before using it, we need to check whether the json extension has been enabled. You can view the current PHP extension status through the phpinfo() function, as shown below:

Execute the above code to view PHP information in the browser. If the json extension has been enabled, you will find content similar to the following on the page:

json
json support => enabled

If it is not enabled, you need to add the following line to the php.ini configuration file:

extension=json.so

After , we can use PHP's built-in json_encode() function to convert the PHP array or object into a json format string. The following is a simple example:

 "John Smith",
    "age" => 30,
    "city" => "New York"
);
echo json_encode($person);
?>

Execute the above code, and the output result is as follows:

{"name":"John Smith","age":30,"city":"New York"}

As you can see, the json_encode() function successfully converted the $person array into characters in json format. string. During the conversion process, if the data contains special characters, such as double quotes, slashes, etc., the json_encode() function will automatically escape them. If the data contains Chinese characters, it needs to be encoded first to avoid garbled characters. You can use the urlencode() function to encode Chinese characters.

In addition to arrays, we can also convert PHP objects into json format strings. Just convert the object into an array. For example:

name = $name;
        $this->age = $age;
        $this->city = $city;
    }
}
$person = new Person("John Smith", 30, "New York");
echo json_encode($person);
?>

The output result is the same as the previous example:

{"name":"John Smith","age":30,"city":"New York"}

Sometimes, we want to include Chinese characters in the output JSON string instead of the encoded characters. The JSON_UNESCAPED_UNICODE option can be added to the json_encode() function to output unprocessed Chinese characters. For example:

 "张三",
    "age" => 30,
    "city" => "北京"
);
echo json_encode($person, JSON_UNESCAPED_UNICODE);
?>

The output results are as follows:

{"name":"张三","age":30,"city":"北京"}

In addition to converting PHP data into JSON format, we can also convert JSON format data into PHP variables. This can be achieved using the json_decode() function. For example:

The output result is as follows:

object(stdClass)#1 (3) {
  ["name"]=>
  string(10) "John Smith"
  ["age"]=>
  int(30)
  ["city"]=>
  string(8) "New York"
}

As you can see, the json_decode() function successfully converts the JSON format string into a PHP object. It should be noted that during the conversion process, the function will automatically convert the data type in JSON into the corresponding PHP data type.

To summarize, PHP provides a wealth of functions and extensions. We can easily convert PHP variables into JSON format data and use it on the front end. At the same time, we can also convert JSON format data into PHP variables for further processing on the backend. For those who use PHP for web development, mastering these functions and extensions will greatly improve development efficiency and quality.

The above is the detailed content of Convert php to json. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn