Home > Backend Development > PHP Tutorial > Convert variables to JSON format string using PHP function 'json_encode'

Convert variables to JSON format string using PHP function 'json_encode'

PHPz
Release: 2023-07-24 10:44:01
Original
1027 people have browsed it

Use the PHP function "json_encode" to convert variables into JSON format strings

When using PHP to develop websites or applications, it is often necessary to convert variables into JSON format strings to facilitate data processing on the front end. Transmission and processing. PHP provides a very convenient function "json_encode" to implement this function.

The "json_encode" function can convert PHP arrays or objects into JSON format strings. Below is some sample code showing how to use the "json_encode" function to convert a variable into a JSON format string.

Example 1: Convert an array to a JSON format string

$fruits = array("apple", "banana", "orange");

$jsonString = json_encode($fruits);

echo $jsonString;
Copy after login

Output:

["apple","banana","orange"]
Copy after login

Example 2: Convert an associative array to a JSON format string

$person = array(
    "name" => "Tom",
    "age" => 25,
    "city" => "New York"
);

$jsonString = json_encode($person);

echo $jsonString;
Copy after login

Output:

{"name":"Tom","age":25,"city":"New York"}
Copy after login
Copy after login

Example 3: Convert object to JSON format string

class Person {
    public $name;
    public $age;
    public $city;
}

$person = new Person();
$person->name = "Tom";
$person->age = 25;
$person->city = "New York";

$jsonString = json_encode($person);

echo $jsonString;
Copy after login

Output:

{"name":"Tom","age":25,"city":"New York"}
Copy after login
Copy after login

In addition to converting the variable to JSON format string," The json_encode" function also provides some parameters to control the output format. For example, you can use the "JSON_PRETTY_PRINT" parameter to make the output string more readable:

$person = array(
    "name" => "Tom",
    "age" => 25,
    "city" => "New York"
);

$jsonString = json_encode($person, JSON_PRETTY_PRINT);

echo $jsonString;
Copy after login

Output:

{
    "name": "Tom",
    "age": 25,
    "city": "New York"
}
Copy after login

Summary:

With the help of PHP functions" json_encode", we can easily convert variables into JSON format strings for data transmission and processing on the front end. Whether it is an array, associative array or object, it can be easily converted to JSON format. According to needs, we can also control the output format by adjusting parameters. Therefore, it is very important to master the usage of "json_encode" function. I hope the sample code in this article can be helpful to you!

The above is the detailed content of Convert variables to JSON format string using PHP function 'json_encode'. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template