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

How to convert json into array in php

藏色散人
藏色散人Original
2021-03-22 09:18:592645browse

In PHP, you can convert json into an array through the json_decode function. The function of this function is to decode the string in JSON format. Its syntax is "json_decode ( string $json , bool $assoc=false.. .)".

How to convert json into array in php

The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer

json_decode

(PHP 5 >= 5.2.0, PHP 7, PHP 8, PECL json >= 1.2.0)

json_decode — Decode JSON format string

Description

json_decode ( string $json , bool $assoc = false , int $depth = 512 , int $options = 0 ) : mixed

Accepts a JSON encoded string and converts it into a PHP variable

Parameters

json

The json string format string to be decoded .

This function can only handle UTF-8 encoded data.

Note:

PHP implements a superset of JSON.

assoc

When this parameter is true, array will be returned instead of object.

depth

Specifies the recursion depth.

options

Mask consisting of JSON_BIGINT_AS_STRING, JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_OBJECT_AS_ARRAY, JSON_THROW_ON_ERROR. The behavior of these constants is further described on the JSON constants page.

Return Value

Returns data encoded in json via the appropriate PHP type. The values ​​true, false and null will return true, false and null accordingly. If json cannot be decoded, or the encoded data depth exceeds the recursion limit, null will be returned.

[Recommended learning: PHP video tutorial]

Example #1 Example of json_decode()

<?php
$json = &#39;{"a":1,"b":2,"c":3,"d":4,"e":5}&#39;;
var_dump(json_decode($json));
var_dump(json_decode($json, true));
?>

The above routine Will output:

object(stdClass)#1 (5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}
array(5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

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