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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:36:32+00:00 2026-05-25T13:36:32+00:00

Stumped by this one: I have a function that returns an array of folders

  • 0

Stumped by this one:

I have a function that returns an array of folders in a given directory. When I iterate through the array, I can see all the items. However, if I try to print the first item, I get nothing:

function get_folders($dir) {
    return array_filter(scandir($dir), function ($item) use ($dir) {
        return (is_dir($dir.'/'.$item) && $item != "." && $item != "..") ;
    });
}
$folders = get_folders(".");
$first_folder = $folders[0]; 
echo $first_folder; // returns blank.

Interestingly, if I don’t filter out the “.” and “..”, then $first_folder does print “.”. Can anyone explain this?

  • 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-05-25T13:36:32+00:00Added an answer on May 25, 2026 at 1:36 pm

    From the array_filter manual:

    Iterates over each value in the input array passing them to the callback function. If the callback function returns true, the current value from input is returned into the result array. Array keys are preserved.

    So, scandir($dir) gets you an array with . at key 0, thus if you later filter . out, there will be nothing at key 0 in the result.

    If you want to know what the first key of the resulting array is, try the array_keys function.

    EDIT: actually, salathe’s suggestion to use array_values is better in your case than getting the keys from array_keys.

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

Sidebar

Related Questions

I'm completely stumped on this one. I have three different lists that need to
Ok, I'm officially stumped on this one. I have a GregorianCalendar object that I
I'm a little stumped on this one. Anyone have any ideas? I'll try to
I'm stumped on this one right now. What I have: public abstract class Class1<T>
Okay, I am stumped on this one and need some quick help. I have
A little stumped by this one so I'm hoping you guys can help. Looks
So I'm stumped on this one. In Mac OS X there is an easy
I'm really stumped by this one! The StackFrame object ( MSDN Link ) has
I'm a little stumped on this one and I'm also not still on 1.8
I'm following a couple of Pythone exercises and I'm stumped at this one. #

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.