json_decode()
function to convert the JSON encoded string into appropriate PHP data type.By default the
json_decode()
function returns an object.<?php // Store JSON data in a PHP variable $json = '{"Peter":65,"Harry":80,"John":78,"Clark":90}'; var_dump(json_decode($json)); ?>
output:
object(stdClass)#1 (4) { ["Peter"]=> int(65) ["Harry"]=> int(80) ["John"]=> int(78) ["Clark"]=> int(90) }
you can optionally specify a second parameter
$assoc
which accepts a boolean value that when set as true
JSON objects are decoded into associative arrays. It is false
by default.<?php // Store JSON data in a PHP variable $json = '{"Peter":65,"Harry":80,"John":78,"Clark":90}'; var_dump(json_decode($json, true)); ?>
array(4) { ["Peter"]=> int(65) ["Harry"]=> int(80) ["John"]=> int(78) ["Clark"]=> int(90) }
No hay comentarios:
Publicar un comentario