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

How to convert json string to array in php

王林
王林Original
2020-09-29 09:23:345517browse

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]]])

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

Output result:

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

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!

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