Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to build a array that contains a folder structure?
    primarykey
    data
    text
    <p>I want to build a Json output to list folder structure inside ExtJS tree panel. The structure should be the equivalent of this array in Json:</p> <pre><code>Array ( [text] =&gt; . [children] =&gt; Array ( [0] =&gt; Array ( [text] =&gt; files [children] =&gt; ) [1] =&gt; Array ( [text] =&gt; folder 1 [children] =&gt; ) [2] =&gt; Array ( [text] =&gt; New directory [children] =&gt; array( [0] =&gt; Array ( [text] =&gt; sub_1 [children] =&gt; array( [0] =&gt; Array ( [text] =&gt; sub_1_1 [children] =&gt; ) [1] =&gt; Array ( [text] =&gt; sub_1_2 [children] =&gt; ) ) ) [1] =&gt; Array ( [text] =&gt; sub_2 [children] =&gt; ) ) ) ) ) </code></pre> <p>i made this function which shows the structure by going through PHP Manual and examples</p> <pre><code>listFolders('../file_uploads/'); function listFolders($dir){ $dh = scandir($dir); echo '&lt;ul&gt;'; foreach($dh as $folder){ if($folder != '.' &amp;&amp; $folder != '..') { if(is_dir($dir.'/'.$folder)){ echo '&lt;li&gt;'.$folder.'&lt;/li&gt;'; listFolders($dir.'/'.$folder); } } } echo '&lt;/ul&gt;'; } </code></pre> <p>this outputs the structure</p> <ul><li>New directory</li><ul><li>sub_1</li><ul><li>sub_1_1</li><ul><li>sub_1_1_1</li><ul></ul></ul><li>sub_1_2</li><ul></ul></ul><li>sub_2</li><ul></ul></ul><li>files</li><ul></ul><li>folder 1</li><ul></ul></ul> <p>I want to know how to convert this output to a array (or the Json) like above?</p> <p><strong>solution</strong></p> <pre><code>print "&lt;pre&gt;"; print_r(listFolders('../file_uploads/')); function listFolders($dir) { $dh = scandir($dir); $return = array(); foreach ($dh as $folder) { if ($folder != '.' &amp;&amp; $folder != '..') { if (is_dir($dir . '/' . $folder)) { $return[] = array( 'text' =&gt; $folder, 'children' =&gt; listFolders($dir . '/' . $folder) ); } } } return $return; } </code></pre> <p><img src="https://i.stack.imgur.com/ODxCJ.png" alt="enter image description here"></p>
    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.
 

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