Home > Backend Development > PHP Problem > How to convert json string to array in php

How to convert json string to array in php

王林
Release: 2023-03-06 08:46:02
Original
5519 people have browsed it

php method to convert a json string into an array: You can use the json_decode function, such as [json_decode($json, true);]. The json_decode function can accept a json format string and convert it into a php variable.

How to convert json string to array in php

Function introduction:

(Recommended tutorial: php video tutorial)

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

Grammar format:

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

Code implementation:

<?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

Output result:

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

Related recommendations: php training

The above is the detailed content of How to convert json string to array in 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