Home > Backend Development > PHP Problem > How to convert json value to array or object in php

How to convert json value to array or object in php

青灯夜游
Release: 2023-03-16 10:56:02
Original
3016 people have browsed it

In PHP, you can use json_decode() to convert json data into an array or object type, the syntax is "json_decode($json,$assoc)"; when the parameter "$assoc" is omitted, json will be converted into Object, when the value of this parameter is set to "TRUE", json will be converted into an array.

How to convert json value to array or object in php

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

In PHP, you can use the json_decode() function Decodes JSON data and converts JSON data into the appropriate PHP data type (object or array).

Conversion syntax:

json_decode($json,$assoc)
Copy after login
  • $assoc parameter can be omitted, the default value is false;

    If this parameter is omitted, the JSON data will be converted into an object

    If the value is set to TRUE, it will be converted to an array.

Example 1: Convert json value to object

<?php
header("Content-type:text/html;charset=utf-8");
$json = &#39;{"a":"php","b":"mysql","c":3}&#39;; 
var_dump($json); 
$obj=json_decode($json);   
var_dump($obj);
?>
Copy after login

How to convert json value to array or object in php

Example 2: Convert json value to array

<?php
header("Content-type:text/html;charset=utf-8");
$json = &#39;{"a":"php","b":"mysql","c":3}&#39;; 
var_dump($json); 
$arr=json_decode($json,TRUE);   
var_dump($arr);
?>
Copy after login

How to convert json value to array or object in php

Recommended learning: "PHP Video Tutorial"

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