Home  >  Article  >  Backend Development  >  Detailed explanation of usage examples of php json related functions

Detailed explanation of usage examples of php json related functions

黄舟
黄舟Original
2017-03-29 09:04:171324browse

This article mainly introduces php json related functions usage, lists the functions of json_encode, json_decode and json_last_error, and analyzes json_encode and json_decode with examples For specific usage skills of functions, friends in need can refer to

This article describes the usage of php json related functions with examples. Share it with everyone for your reference, the details are as follows:

Function list:

##json_last_errorReturn the last occurrence Error
Function Description
json_encode Encode json on variable
json_decode Decode the string in json format and convert it into a php variable

##For example 1:
json_encode

$arr=array("A"=>"a","B"=>"b","C"=>"c","D"=>"d");
echo json_encode($arr);
output:

{"A":"a","B":"b","C":"c","D":"d"}

For example 2:

json_decode

$arr='{"A":"a","B":"b","C":"c","D":"d"}';
var_dump(json_decode($arr));
var_dump(json_decode($arr,true));
output:

object(stdClass)[1]
 public 'A' => string 'a' (length=1)
 public 'B' => string 'b' (length=1)
 public 'C' => string 'c' (length=1)
 public 'D' => string 'd' (length=1)
array (size=4)
 'A' => string 'a' (length=1)
 'B' => string 'b' (length=1)
 'C' => string 'c' (length=1)
 'D' => string 'd' (length=1)

The above is the detailed content of Detailed explanation of usage examples of php json related functions. 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