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 7791051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:46:08+00:00 2026-06-01T21:46:08+00:00

Does anyone have a brilliant idea how to obtain the elements with the deepest

  • 0

Does anyone have a brilliant idea how to obtain the elements with the deepest path from an array with file paths? If this sounds weird, imagine the following array:

/a/b
/a
/1/2/3/4
/1/2
/1/2/3/5
/a/b/c/d/e

What I want to obtain is:

/1/2/3/4
/1/2/3/5
/a/b/c/d/e

Wondering what the fastest method is without having to iterate over the whole array over and over again. Language is PHP (5.2).

  • 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-01T21:46:10+00:00Added an answer on June 1, 2026 at 9:46 pm

    Following your clarifications, here’s a function that would do it. It keeps an array of the “deepest paths” found and compares each path against it. The best-case scenario is O(n) (if all paths are subpaths of the largest one) and worst-case scenario is O(n2) (if all paths are completely distinct).

    Note that continue 2 means “continue on the outer loop”.

    <?php
    
    function getDeepestPaths($array)
    {
        $deepestPaths = array();
        foreach ($array as $path)
        {
            $pathLength = strlen($path);
            // look for all the paths we consider the longest
            // (note how we're using references to the array members)
            foreach ($deepestPaths as &$deepPath)
            {
                $deepPathLength = strlen($deepPath);
                // if $path is prefixed by $deepPath, this means that $path is
                // deeper, so we replace $deepPath with $path
                if (substr($path, 0, $deepPathLength) == $deepPath)
                {
                    $deepPath = $path;
                    continue 2;
                }
                // otherwise, if $deepPath is prefixed by $path, this means that
                // $path is shallower; so we should stop looking
                else if (substr($deepPath, 0, $pathLength) == $path)
                {
                    continue 2;
                }
            }
            // $path matches nothing currently in $deepestPaths, so we should
            // add it to the array
            $deepestPaths[] = $path;
        }
        return $deepestPaths;
    }
    
    $paths = array('/a/b', '/a', '/1/2/3/4', '/1/2', '/1/2/3/5', '/a/b/c/d/e');
    print_r(getDeepestPaths($paths));
    
    ?>
    

    If your folder names don’t end with slashes, you’ll want to do an additional check in the two ifs: that the character next to the prefix in the deeper path is a slash, because otherwise a path like /foo/bar will be seen as a “deeper path” than /foo/b (and will replace it).

    if (substr($path, 0, $deepPathLength) == $deepPath && $path[$deepPathLength] == '/')
    if (substr($deepPath, 0, $path) == $path && $deepPath[$path] == '/')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have any idea why my server cannot read the database file? No
Does anyone have a idea how to do this kind progress bar?
Does anyone have any idea how to change the thickness of the text pointer
Does anyone have some code that will take a TimeZoneInfo field from .NET and
Does anyone have any idea to track event of google analytics programatically in asp.net?
Does anyone have any idea why the following causes the program to crash? NSFileManager
Does anyone have a query that will select n number of random rows from
Does anyone have any idea why money_format() is showing � instead of pound sign
Does anyone have some sample code demonstrating how to make a file browser view?
Does anyone have any information on this? I'm currently running Visual Studio Team System

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.