title: How to send an array of objects in JSON format from PHP
body: <p>New to php. I am trying to send JSON data to front end in name-value pair. I tried an example which I got <a href="http://php.net/manual/en/function.json-encode.php" rel="noreferrer">here</a>  The following is my code fragment which sends the data in JSON name value format.</p>
  
  <pre><code>while($stmt-&gt;fetch()){
      $list = array('id' =&gt; $fid, 'name' =&gt; $fname);
  }
  $stmt-&gt;free_result();
  $stmt-&gt;close();
  echo json_encode($list);
  </code></pre>
  
  <p>I got this on front-end</p>
  
  <pre><code>Object {id: 12, name: "Manisha"}
  </code></pre>
  
  <p>The problem is I was expecting an array of objects. The above value is the last value obtained from the SQL query. What are the alterations I should make to this code so that I can get an array of objects. Something like </p>
  
  <pre><code>[{"id":"1","name":"Kumari"}, {"id":"2","name":"KM"}, {"id":"3","name":"Manisha"}]
  </code></pre>
  
  <p>Please advice.</p>