Home > Backend Development > PHP Tutorial > How to use php json_encode

How to use php json_encode

angryTom
Release: 2023-04-07 16:28:02
forward
5548 people have browsed it

How to use php json_encode

In current interface development, there are very few xml formats, and most of them have been replaced by json. PHP, which is known as a language specially developed for interfaces, naturally has its powerful characteristics. That is its built-in function json_encode.

The function prototype is function json_encode($value,$option=0)

where $value is the data we want to convert to json. It can be said that the array and object. (The requirement is utf8 encoding)

$option is the conversion parameter. A binary mask composed of the following constants:

JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, among which we often use the JSON_FORCE_OBJECT parameter, which is to force the data to be converted to j son object .

Let’s take an example below

$arr = ['name'=>'梦回故里','age'=>'18'];
die(json_encode($arr));
Copy after login

The execution result is:

How to use php json_encode

class A{
      public $name = "";
      public $age = "";
  }
  $a = new A();
  $a->name = "梦回故里";
  $a->age= "18";
die(json_encode($a));
Copy after login

How to use php json_encode

This is the result of my operation.

For more PHP related knowledge, please visit PHP Chinese website!

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

Related labels:
source:www.80shihua.com
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