Enter to elements of a Json object with php
<p>I've got the following json object:</p>

<pre><code>$dati = array(
    "data" =&gt; array(
        'address_complete'=&gt;$data-&gt;results[0]-&gt;formatted_address, 
        'address_square'=&gt;$data-&gt;results[0]-&gt;address_components[1]-&gt;long_name,
        'location'=&gt;$data-&gt;results[0]-&gt;address_components[2]-&gt;long_name,
        'postal_code'=&gt;$data-&gt;results[0]-&gt;address_components[7]-&gt;long_name,
        'data_ora'=&gt;$tmp_date
    ),
    "str" =&gt; array ("n1"=&gt;"fabio",
                    "n2"=&gt;"marcolino",
                    "n3"=&gt;"lauretta")
);
$p =  json_encode($dati);
echo $p;
</code></pre>

<p>How can I enter the <code>n1</code> element to show <code>fabio</code> as output?</p>

<p><code>$p.str.n1</code> doesn't work. How can I do this?</p>

<p>Besides, how can I show <code>fabio</code>,<code>marcolino</code>,<code>lauretta</code> using a <code>for</code> loop?</p>

<pre><code>for ($i=1; $i&lt;=3; $i++) {
    $dati = array(
      "data" =&gt; array(
        'address_complete'=&gt;$data-&gt;results[0]-&gt;formatted_address, 
        'address_square'=&gt;$data-&gt;results[0]-&gt;address_components[1]-&gt;long_name,
        'location'=&gt;$data-&gt;results[0]-&gt;address_components[2]-&gt;long_name,
        'postal_code'=&gt;$data-&gt;results[0]-&gt;address_components[7]-&gt;long_name,
        'data_ora'=&gt;$tmp_date
      ),
      "str" =&gt; array (
        "n".$i=&gt;"fabio",
        "n".$i=&gt;"marcolino",
        "n".$i=&gt;"lauretta") 
    );
    //$p =  json_encode($dati);
    echo $dati[str]["n".$i];
}
</code></pre>

<p>How can I fix this code?</p>

<hr>

<p>Finally I've got another question: if <code>"str" =&gt; array ("n1"=&gt;"fabio", "n2"=&gt;"marcolino", "n3"=&gt;"lauretta")</code> are saved in a database and I want to get them from it, is correct to write in the following way?</p>

<pre><code>"str" =&gt; array("n".$i=&gt;"Ti sei incrociato con ".$array_db[username]),
</code></pre>

<p>and to call them with Ajax: </p>

<pre><code>... success:function(msg){
        if(msg){
           $("#location").html(Object.keys(msg.str).map(x =&gt; msg.str[x]).join(", "));
        }else{
            $("#location").html('Not Available');
        }
</code></pre>