Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The second argument of <code>json_decode</code> tells the function whether to return the data as an object, or an array.</p> <p>Object access uses the <code>-&gt;</code> symbol. To return an object from <code>json_decode</code> either use <code>json_decode($jsonString)</code> or <code>json_decode($jsonString, false)</code> (the second argument is <a href="http://php.net/manual/en/function.json-decode.php">false by default</a>)</p> <pre><code>$jsonString = '{ "this_is_json" : "hello!" }'; $obj = json_decode($jsonString); echo $obj-&gt;this_is_json // "hello!"; </code></pre> <p>You can also access your json data as an array by setting the second argument to <code>true</code> </p> <pre><code>$jsonString = '{ "this_is_json" : "hello!" }'; $arr = json_decode($jsonString, true); echo $arr['this_is_json'] // "hello!"; </code></pre> <p>What can be a little more conceptually confusing, is that PHP <code>json_decode</code> can return either an array of objects (rather than just an object), or an associative array.</p> <p>Consider the following json string. This string represents a "collection" (square brackets) of json data structures (curly braces).</p> <pre><code>[ { "name": "One" }, { "name": "Two" } ] </code></pre> <p>If we assign this json to the variable <code>$string</code> hopefully this will illustrate the difference</p> <pre><code>$asObjects = json_decode($string); $asAssociativeArray = json_decode($string, true); foreach ($asObjects as $obj) { echo $obj-&gt;name; } foreach ($asAssociativeArray as $arr) { echo $arr['name']; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload