Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8136237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:42:03+00:00 2026-06-06T10:42:03+00:00

I have an array: Array ( [0] => dir0|file0.txt [1] => dir0|file1.txt [2] =>

  • 0

I have an array:

Array
(
    [0] => dir0|file0.txt
    [1] => dir0|file1.txt
    [2] => dir1|file2.txt
    [3] => dir1|filea.txt
    [4] => dir2|fileb.txt
)

I would like it to be displayed as a tree, such as:

dir0
  file0.txt
  file1.txt
dir1 
  file2.txt
  filea.txt
dir2
  fileb.txt

Can any one explain how I can do that?

Edit: Updated for multidimentional array:

$paths[0][0] = 'dir0|file0.txt';
$paths[0][1] = 400;
$paths[1][0] = 'dir0|filea.txt';
$paths[1][1] = 500;
$paths[2][0] = 'dir1|file1.txt';
$paths[2][1] = 600;
$paths[3][0] = 'dir1|fileb.txt';
$paths[3][1] = 700;
$paths[4][0] = 'dir2|filec.txt';
$paths[4][1] = 700;

I would like the output to be:

dir0 (900)
  file0.txt (400)
  filea.txt (500)
dir1 (1300)
  file1.txt(600)
  fileb.txt (700)
dir2 (700)
  filec.txt (700)

The values need to be added and displayed in root.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T10:42:05+00:00Added an answer on June 6, 2026 at 10:42 am

    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:

    $array = array( ..);
    $reformatted = array();
    foreach( $array as $k => $v) {
        list( $key, $value) = explode( '|', $v);
        if( !isset( $reformatted[$key])) 
            $reformatted[$key] = array();
        $reformatted[$key][] = $value;
    }
    

    Then you just have to iterate over the new array, like so:

    foreach( $reformatted as $dir => $files) {
        echo $dir . "\n";
        foreach( $files as $file)
            echo "\t" . $file . "\n";
    }
    

    This outputs:

    dir0
        file0.txt
        file1.txt
    dir1
        file2.txt
        filea.txt
    dir2
        fileb.txt
    

    Note that this will only work for plain text environment (such as <pre></pre>, like in the demo). Otherwise, you’ll need to use <br /> instead of \n for line breaks, or use an ordered or unordered list.

    For HTML output, use this, whose output can be seen here

    echo '<ul>';
    foreach( $reformatted as $dir => $files) {
        echo "<li>$dir</li>";
        echo '<ul>';
        foreach( $files as $file)
            echo "<li>$file</li>";
        echo '</ul>';
    }
    echo '</ul>';
    

    Generates:

    • dir0
    • file0.txt
    • file1.txt
  2. dir1
    • file2.txt
    • filea.txt
  3. dir2
    • fileb.txt

    For your updated array, here is the solution:

    $reformatted = array(); $weights = array();
    foreach( $paths as $k => $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 => $files) {
        echo $dir . ' (' . $weights[$dir] . ")\n";
        foreach( $files as $file)
            echo "\t" . $file[0] . ' (' . $file[1] . ")\n";
    }
    

    This outputs:

    dir0 (900)
        file0.txt (400)
        filea.txt (500)
    dir1 (1300)
        file1.txt (600)
        fileb.txt (700)
    dir2 (700)
        filec.txt (700)
    

    I’ll leave it up to you to translate that into HTML if necessary.

  • 0
  • Reply
  • Share
    Share
    • Share on Facebook
    • Share on Twitter
    • Share on LinkedIn
    • Share on WhatsApp
    • Report

Sidebar

Related Questions

I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
I have array result like this: Array ( [0] => stdClass Object ( [id_global_info]
An array is defined of assumed elements like I have array like String[] strArray
Hello i have array like this [0] => Array ( [ExamMonth] => Array (
Have an array of chars like char members[255]. How can I empty it completely
I have array like below, Array ( [14289] => Array ( [0] => Karthikeyan
so I have array like ParamsArray {a,b,a,a,...b} (so i have 2 kinds of parameters
i have array like below which is sorted by array_count_values function, Array ( [Session
I have array like below: Array ( [_edit_lock] => Array ( [0] => 1309611144:1
I have array like this: $test = array(20, 30, 40); And I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.