title: how to return json from a php function
body: <p>have written a class that uses in built php function DirectoryIterator
  which as to show folder structure like this</p>
  
  <pre><code>root
     |__subfolder
     |          |_mp3 
     |          |_png
     |__subfolder2
                |_mp3
                |_mp3 
  </code></pre>
  
  <p>Here is my class </p>
  
  <pre><code> &lt;?php
    $list = array();
  
   $dir = new DirectoryIterator('/opt/lampp');
   foreach ($dir as $fileinfo)
   {
   if ($fileinfo-&gt;isFile())
    {
      //echo $fileinfo-&gt;getBasename() . "\n";
     //echo $fileinfo-&gt;getBasename('.jpg') . "\n";
    }
    else
     { //create object with two fields
              $list3 = array( 'name'=&gt; $fileinfo-&gt;getFilename(),
                              'type'=&gt; $fileinfo-&gt;getType(),
                              'date'=&gt; $fileinfo-&gt;getMTime(),
                              'size'=&gt; $fileinfo-&gt;getSize());
               array_push($list, $list3);
  
  
  
      }
  
  
          $return_array = array('files'=&gt;$list);
          echo json_encode(  $return_array)
       }
       ?&gt;
  </code></pre>
  
  <p>have written the echo json_encode but the string format does not seem to be correctly writen </p>
  
  <p>here is a view of the string format of the out put, am using online json viewer for verification</p>
  
  <pre><code> {"files":[{"name":"info","type":"dir","date":1491647493,"size":4096}]}     {"files":[{"name":"info","type":"dir","date":1491647493,"size":4096},{"name":"pear","type":"dir","date":1491647422,"size":4096}]}{"files":[{"name":"info","type":"dir","date":1491647493,"size":4096},{"name":"pear","type":"dir","date":1491647422,"size":4096},
  </code></pre>