Convert JSON string to object in PHP

json_decode — Decodes a JSON string. Takes a JSON encoded string and converts it into a PHP variable.

Returns the value encoded in json in appropriate PHP type. Values true, false and null are returned as TRUE, FALSE and NULL respectively. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.

Eg:
<?php
$json = '
    {
        "color":"red",
        "font-size":"12px",
        "background-color":"#F5F5F5",
        "height":"40px",
        "width":"100%"
    }';
var_dump(json_decode($json));
var_dump(json_decode($json, true));
?>
Output:

object(stdClass)#196 (5) {
  ["color"]=>
  string(3) "red"
  ["font-size"]=>
  string(4) "12px"
  ["background-color"]=>
  string(7) "#F5F5F5"
  ["height"]=>
  string(4) "40px"
  ["width"]=>
  string(4) "100%"
}
array(5) {
  ["color"]=>
  string(3) "red"
  ["font-size"]=>
  string(4) "12px"
  ["background-color"]=>
  string(7) "#F5F5F5"
  ["height"]=>
  string(4) "40px"
  ["width"]=>
  string(4) "100%"
}

Comments

Popular posts from this blog

Gove confirms mandatory housebuilding targets for councils will be abolished in face of Tory rebellion – UK politics live

Kotak Mahindra Bank Recruitment 2022 Released for Graduate Candidates And Apply Online