Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The best way would be to reformat the array so the keys were the directories, and the array values were arrays containing file names, like so:</p> <pre><code>$array = array( ..); $reformatted = array(); foreach( $array as $k =&gt; $v) { list( $key, $value) = explode( '|', $v); if( !isset( $reformatted[$key])) $reformatted[$key] = array(); $reformatted[$key][] = $value; } </code></pre> <p>Then you just have to iterate over the new array, like so:</p> <pre><code>foreach( $reformatted as $dir =&gt; $files) { echo $dir . "\n"; foreach( $files as $file) echo "\t" . $file . "\n"; } </code></pre> <p>This <a href="http://viper-7.com/2kXKoE" rel="nofollow">outputs</a>:</p> <pre><code>dir0 file0.txt file1.txt dir1 file2.txt filea.txt dir2 fileb.txt </code></pre> <p>Note that this will only work for plain text environment (such as <code>&lt;pre&gt;&lt;/pre&gt;</code>, like in the demo). Otherwise, you'll need to use <code>&lt;br /&gt;</code> instead of <code>\n</code> for line breaks, or use an ordered or unordered list.</p> <p><strong>For HTML output, use this,</strong> whose output can be <a href="http://viper-7.com/jBVGjX" rel="nofollow">seen here</a></p> <pre><code>echo '&lt;ul&gt;'; foreach( $reformatted as $dir =&gt; $files) { echo "&lt;li&gt;$dir&lt;/li&gt;"; echo '&lt;ul&gt;'; foreach( $files as $file) echo "&lt;li&gt;$file&lt;/li&gt;"; echo '&lt;/ul&gt;'; } echo '&lt;/ul&gt;'; </code></pre> <p>Generates: </p> <ul><li>dir0</li><ul><li>file0.txt</li><li>file1.txt</li></ul><li>dir1</li><ul><li>file2.txt</li><li>filea.txt</li></ul><li>dir2</li><ul><li>fileb.txt</li></ul></ul> <p>For your updated array, here is the solution:</p> <pre><code>$reformatted = array(); $weights = array(); foreach( $paths as $k =&gt; $v) { list( $key, $value) = explode( '|', $v[0]); if( !isset( $reformatted[$key])) $reformatted[$key] = array(); if( !isset( $weights[$key])) $weights[$key] = 0; $reformatted[$key][] = array( $value, $v[1]); $weights[$key] += $v[1]; } foreach( $reformatted as $dir =&gt; $files) { echo $dir . ' (' . $weights[$dir] . ")\n"; foreach( $files as $file) echo "\t" . $file[0] . ' (' . $file[1] . ")\n"; } </code></pre> <p>This <a href="http://viper-7.com/HFAFjX" rel="nofollow">outputs</a>:</p> <pre><code>dir0 (900) file0.txt (400) filea.txt (500) dir1 (1300) file1.txt (600) fileb.txt (700) dir2 (700) filec.txt (700) </code></pre> <p>I'll leave it up to you to translate that into HTML if necessary.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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