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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:39:50+00:00 2026-06-13T11:39:50+00:00

I want to return multiple values from a PHP function, but it did not

  • 0

I want to return multiple values from a PHP function, but it did not work as following codes:

The function is used to search the filenames in the specific folder and its recursive folders and store the filenames in the array.

In this example, the specific (main) folder is called: F:\test
The recursive folder is called: F:\test\subfolder

The main folder and subfolder carries 7 files, the filename formats are:

For main folder: 1.txt, 2.txt, 3.txt, 4.txt
For subfolder: 5.txt, 6.txt, 7.txt

function getDirectory( $path = '.', $level = 0 ) {
$i=0;$j=0;

$dh = @opendir( $path );
while( false !== ( $file = readdir( $dh ) ) ){
// Loop through the directory

        if( is_dir( "$path/$file" ) ){
        // Its a directory, so we need to keep reading down...

            getDirectory( "$path/$file", ($level+1) );
            // Re-call this same function but on a new directory, this is what makes function recursive.

        } else {
            if ($level>0) //in a recursive folder
            {
                $dir_matched[$j]=$file;
                $j++;
            }
            else //in main folder
            {
            $files_matched[$i] = $file;
            $i++;
            }               
        }    
}
closedir( $dh );
//print_r ($files_matched);
//print_r ($dir_matched);   I tested this before return, both works fine.

return array($files_matched,$dir_matched);
}



echo "<pre>";
list($a,$b) = getDirectory("F:\test");
print_r ($a);   // this will result the same as array $files_matched, it ok!
print_r ($b);   // but i don't know why I cannot get the array of $dir_matched??
echo "</pre>";   

As you can see, it is so strange that I can only get one array only? Is there any idea I can get the contents of the $dir_matched array?

  • 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-13T11:39:51+00:00Added an answer on June 13, 2026 at 11:39 am

    The way it is written now, you are not capturing the values from the recursive call. Within your function, on this line:

    getDirectory( "$path/$file", ($level+1) );
    

    You need to capture the returned values from it. Something like:

    $files_matched[++$i] = getDirectory( "$path/$file", ($level+1));
    

    $i may not what you want here, you’ll need to increment it here also like you do in your else statement, or else capture them in a different variable to reflect a subdirectory – depends on what you want to accomplish.

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

Sidebar

Related Questions

I want to return a set of values from function till the point they
I want to return multiple values from a method and I've decided to use
I have the following code to return multiple values from pl/python: CREATE TYPE named_value
I want to return a private field from a remoted object but i get
I'm creating multiple form retrieving data from jquery. after that i want to work
How would I return multiple values (say, a number and a string) from a
I have situation where I want to extract multiple values from multiple source objects
I want to return nothing when if condition is not met, for instance (if
I want to return ArrayList<HashMap<String,String>> in EL function with three String argument. How to
I want to return some files in a HttpResponse and I'm using the following

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.