Baza wiedzyCentrum egzaminacyjne
PLPolski
Wybierz język13

CKTIS / PHP

json_decode()

Odczytuje JSON w UTF-8 jako wartości PHP.

Wersja: PHP 8.4; repository runtime, production runtime not attested

Składnia

json_decode(string $json, ?bool $associative = null, int $depth = 512, int $flags = 0): mixed

Odczytuje JSON w UTF-8 jako wartości PHP. Domyślnie obiekt daje stdClass, associative=true daje tablicę, a null podąża za JSON_OBJECT_AS_ARRAY. Poprawny JSON null i błąd składni domyślnie dają null. JSON_THROW_ON_ERROR odróżnia błąd wyjątkiem.

Sygnatura

json_decode(string $json, ?bool $associative = null, int $depth = 512, int $flags = 0): mixed

Wynik przykładu

["stdClass",2,null,"JsonException"]

Przykłady

<?php
$object = json_decode("{\"x\":2}"); $map = json_decode("{\"x\":2}",true);
try { json_decode("[",true,512,JSON_THROW_ON_ERROR); } catch (JsonException $e) { $bad = get_class($e); }
echo json_encode([get_class($object),$map["x"],json_decode("null"),$bad]);

Wynik

["stdClass",2,null,"JsonException"]

Źródła