Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>JSON has two structures: an "array", which has order but no keys, and an "object" (hash) which has keys but no order. PHP, on the other hand, combines these into one structure, which it calls an "array", which has both keys <em>and</em> order. PHP's <code>json_encode</code> function therefore has to choose which JSON structure to use for a particular PHP array.</p> <p>In your data, the PHP array <code>$json['favourites']</code> contains both string keys (e.g. <code>$json["favorites"]["favorites_type"]</code>) and automatically incremented numeric keys (where you have used <code>$json["favorites"][]</code>). Since the string keys can't exist in a JSON array, PHP's <code>json_encode</code> will have to encode this as a JSON object, which is why you see the keys <code>"0":</code>,<code>"1":</code>, etc in the output (those being the same keys you'd see if you used <code>print_r</code> on the array, for instance).</p> <p>If you remove the string keys from that array (moving that data to somewhere else in the structure), then all that is left is the numeric array. Since this is like a JSON array, PHP will output it as such, and you won't see keys in the output.</p> <p>As I pointed out in a comment, the <em>amount</em> of wrapping around each item remains unchanged - the elements do not have "the same names", they simply exist in an ordered structure, rather than an associative one.</p> <p>Here is <a href="http://codepad.viper-7.com/GV2eVD" rel="nofollow">a code demo</a>:</p> <pre><code>$json["favorites"] = array(); $json["favorites"]["favorites_type"] = "lyrics"; // Simlulating entries in loop: $json["favorites"][] = array('favourite' =&gt; array('lyric_id' =&gt; 3530822107858532003)); $json["favorites"][] = array('favourite' =&gt; array('lyric_id' =&gt; 3530822107858535795)); echo "BEFORE:"; echo json_encode($json, JSON_PRETTY_PRINT); // Remove the problematic string key: unset($json["favorites"]["favorites_type"]); echo "&lt;br&gt;"; echo "AFTER:"; echo json_encode($json, JSON_PRETTY_PRINT); </code></pre> <p>Output:</p> <pre><code>BEFORE:{ "favorites": { "favorites_type": "lyrics", "0": { "favourite": { "lyric_id": 3530822107858532003 } }, "1": { "favourite": { "lyric_id": 3530822107858535795 } } } } AFTER:{ "favorites": [ { "favourite": { "lyric_id": 3530822107858532003 } }, { "favourite": { "lyric_id": 3530822107858535795 } } ] } </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.
 

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