Home > Backend Development > PHP Problem > How to convert json to php

How to convert json to php

藏色散人
Release: 2023-03-01 08:26:01
Original
2166 people have browsed it

How to convert json to php

How to convert json to php?

json_decode is a PHP built-in function added after php5.2.0. Its function is to encode strings in JSON format.

The syntax rules of json_decode:

  json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )
Copy after login

json_decode accepts a JSON format string and converts it into a PHP variable. When the parameter $assoc is TRUE, array will be returned, otherwise object will be returned.

String in JSON format

  $json = '{"a":"php","b":"mysql","c":3}';
Copy after login

Where a is the key, and php is the key value of a.

Example:

<?php   
$json = &#39;{"a":"php","b":"mysql","c":3}&#39;;  
$json_Class=json_decode($json);   
$json_Array=json_decode($json, true);   
print_r($json_Class);   
print_r($json_Array);         
?>
Copy after login

Program output:

  stdClass Object (
  [a] => php
  [b] => mysql
  [c] => 3 )
  Array (
  [a] => php
  [b] => mysql
  [c] => 3 )
Copy after login

Under the premise of the above code

Access the value of a of the object type $json_Class

echo $json_Class->{&#39;a&#39;};
Copy after login

Program output: php

Access the value of a in the array type $json_Array

echo $json_Array[&#39;a&#39;];
Copy after login

Program output: php

Related recommendations: "PHP Tutorial"

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

Related labels:
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