-1

I have an small piece of PHP code that needs to put every file in the current directory into an array.

I have done this by making reading the dir with glob() and when it meets another dir it will loop.

My code I have as of now:

<?php
    $find = '*';
    $result = array();

    function find($find)
    {
        foreach (glob($find) as $entry)
        {
            $result[] = $entry;
            echo $entry.'<br>';

            if (is_dir($entry)){
                $zoek = ''.$entry.'/*';
                find($zoek);
            }
        }

        return $result;
    }

    print_r(find($find));
?>

When I execute the code the echo print exactly what I want. But the printed array doesn't give me the values I want, it only gives the values in the first dir it will come by then it seems to stop adding the value in the array.

What am I doing wrong?

3
  • 1
    Could you add the expected vs the actual outcome to better highlight what you would like to achieve? Commented Jan 15, 2019 at 10:52
  • Your recursive call is pretty useless since you don't manage the return value in the calling function. Do something with the return of find($zoek); Commented Jan 15, 2019 at 10:54
  • Your recursive find is not being assigned to anything. Commented Jan 15, 2019 at 10:54

2 Answers 2

2

You need to actually preserve the results you produce in the recursive callings to your function:

<?php
function listNodesInFolder($find) {
    $result = [];
    foreach (glob($find) as $entry) {
        $result[] = $entry;
        if (is_dir($entry)) {
            $result = array_merge($result, find($entry.'/*'));
        }
    }
    return $result;
}

print_r(find('*'));

Once on it I also fixes a few other issues with your code:

  • $result should be declared as an array inside your function, that that even if it does not loop you still return an array and not something undefined.
  • indentation and location of brackets got adjusted to general coding standards, that makes reading your code much easier for others. Get used to those standards, it pays out, you will see.
  • no need for an extra variable for the search pattern inside the conditional.
  • a speaking name for the function that tells what it actually does.
  • you should not name variables and functions alike ("find").
Sign up to request clarification or add additional context in comments.

3 Comments

You forgot the closing tag of the array_merge, exept that great solution thank you!
"indentation and location of brackets got adjusted to general coding standards, that makes reading your code much easier for others." This is a matter of taste. I personnaly find the aligned curly brackets far more readable
@Cid Your are undoubtedly right that the readability is a matter of personal choice, or let's be honest: personal habit, so training. But that is only half the story, since it is important that code can be read by others. If everyone uses his/her personal style/dialect then this does not really work which is why projects, teams, companies agree on coding standards. The more unique those are the lower the migration barrier for both, code and people... But you are right that there is no "best" or "absolute" standard.
1

You need to add the result of find() to the array

Edit added array_merge - Cid's idea

<?php
    $find = '*';

    function find($find)
    {
        $result = array();
        foreach (glob($find) as $entry)
        {
            $result[] = $entry;
            echo $entry.'<br>';

            if (is_dir($entry)){
                $zoek = ''.$entry.'/*';
                $result = array_merge($result, find($zoek));
            }
        }

        return $result;
    }

    print_r(find($find));
?>

2 Comments

Consider using array_merge() instead of $result[] = find($zoek);
Great idea, i'll add it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.