search

Usage of json in php

Oct 12, 2020 am 11:24 AM
jsonphp

Usage of json in php: json_encode() function is used to convert arrays and objects into json format; json_decode() function is used to convert json text into the corresponding php data structure, such as [json_decode($json ,ture)].

Usage of json in php

Starting from version 5.2, PHP natively provides json_encode() and json_decode() functions, the former is used for encoding, and the latter is used for decoding.

(Recommended tutorial: php video tutorial)

json_encode()

This function is mainly used to convert arrays and objects into json format.

Example:

$arr = array ('a'=>'a','b'=>'b','c'='c','d'=>'d','e'='e');
echo json_encode($arr);

json only accepts utf-8 encoded characters, and the parameters of json_encode() must be utf-8 encoded.

class person 
{ 
  public $name; 
  public $age; 
  public $height; 
  function __construct($name,$age,$height) 
  { 
    $this->name = $name; 
    $this->age = $age; 
    $this->height = $height;   
  }  
} 
$obj = new person("zhangsan",20,100); 
$foo_json = json_encode($obj); 
echo $foo_json;

When the attributes in the class are private variables, they will not be output.

json_decode()

This function is used to convert json text into the corresponding PHP data structure.

$json = '{"a":"hello","b":"world","c":"zhangsan","d":20,"e":170}'; 
var_dump(json_decode($json));

Normally, json_decode() always returns a PHP object.

Converted to an array:

$json = '{"a":"hello","b":"world","c":"zhangsan","d":20,"e":170}';
var_dump(json_decode($json,ture));

Related recommendations: php training

The above is the detailed content of Usage of json in php. 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

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor