search

Convert php to json

May 05, 2023 pm 12:44 PM

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:

<?php phpinfo();
?>

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:

<?php $person = array(
    "name" => "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:

<?php class Person {
    public $name;
    public $age;
    public $city;
    function __construct($name, $age, $city) {
        $this->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:

<?php $person = array(
    "name" => "张三",
    "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:

<?php $json_str = &#39;{"name":"John Smith","age":30,"city":"New York"}&#39;;
$person = json_decode($json_str);
var_dump($person);
?>

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools