0

I got directory array below.

I will make a file search in this array. For example path.php , survey.php... If file is found , how should a construct the path.

for path.php

I want function to return '/survey/config/path.php'

Array
(
[survey] => Array
    (
        [config] => Array
            (
                [0] => path.php
                [1] => routes.php
            )

        [controllers] => Array
            (
                [0] => admin.php
                [1] => giris.php
            )

        [models] => Array
            (
                [0] => giris.php
            )

        [views] => Array
            (
                [0] => admin_form.php
                [1] => widget.php
                [2] => yeni_form.php
            )

        [widgets] => Array
            (
                [0] => survey.php
            )

    )

)

1 Answer 1

1
function find_file_path($dir_structure, $filename) {
    foreach($dir_structure as $dir => $subpath) {
        if(is_array($subpath)) {
            $sub_found = find_file_path($subpath, $filename);
            if($sub_found) {
                return "/" . $dir . $sub_found;
            }
        } else {
            if($subpath === $filename) {
                return "/$filename";
            }
        }
    }
    return FALSE;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. But it returns survey/config/0/path.php. How can i remove "0" or "1" ? just survey/config/path.php
If an answer solves your problem, you should click the green checkmark outline next to it to mark it as "Accepted". :)

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.