Home > Back-end >  I cant loop properly through multdimensional array
I cant loop properly through multdimensional array

Time:05-30

I have an multidimesional array of my folders and files. It looks like this:

$arr = array(
    0           => 'file.php',
    1           => 'file2.php',
    'folder1'   => array(
        'subfolder1'    => array(
            0       => 'subfile.php',
            1       => 'subfile2.php',
            2       => 'subfile3.php'
        )
    ),
    'folder2'   => array(
        0       => 'image.jpg',
        1       => 'image2.jpg',
        'subfolder2'    => array(
            0       => 'subimage3.jpg',
            1       => 'subimage4.jpg'
        ),
        'subfolder3'    => array(
            0       => 'subfile4.php',
            1       => 'subfile5.php'
        ),
    ),
);

I am writing a function to loop through this array to get simple list of files with their pathes. Here is function:

function multiarrToList($arr,$list = false,$path = false){
    if(!is_array($list)){
        $list = array();    
    }
    if(!is_array($path)){
        $path[] = '';
    }

    foreach($arr as $key => $value){

        if(is_array($value)){

            $path[] = $key;

            $list = multiarrToList($arr[$key],$list,$path);
        
        } else {
            $list[] = array(
                'filename'      => $value,
                'path'          => $path
            );
        }

    }
    
    return $list;
}

I am stuck because I am looping properly to get each filenames but pathes arent working properly. You can see it in result from item 5 and so on. There is repeat of folder which shouldnt be there int he path. Result:

    Array
(
    [0] => Array
        (
            [filename] => file.php
            [path] => Array
                (
                    [0] => 
                )

        )

    [1] => Array
        (
            [filename] => file2.php
            [path] => Array
                (
                    [0] => 
                )

        )

    [2] => Array
        (
            [filename] => subfile.php
            [path] => Array
                (
                    [0] => 
                    [1] => folder1
                    [2] => subfolder1
                )

        )

    [3] => Array
        (
            [filename] => subfile2.php
            [path] => Array
                (
                    [0] => 
                    [1] => folder1
                    [2] => subfolder1
                )

        )

    [4] => Array
        (
            [filename] => subfile3.php
            [path] => Array
                (
                    [0] => 
                    [1] => folder1
                    [2] => subfolder1
                )

        )

    [5] => Array
        (
            [filename] => image.jpg
            [path] => Array
                (
                    [0] => 
                    [1] => folder1
                    [2] => folder2
                )

        )

    [6] => Array
        (
            [filename] => image2.jpg
            [path] => Array
                (
                    [0] => 
                    [1] => folder1
                    [2] => folder2
                )

        )

    [7] => Array
        (
            [filename] => subimage3.jpg
            [path] => Array
                (
                    [0] => 
                    [1] => folder1
                    [2] => folder2
                    [3] => subfolder2
                )

        )

    [8] => Array
        (
            [filename] => subimage4.jpg
            [path] => Array
                (
                    [0] => 
                    [1] => folder1
                    [2] => folder2
                    [3] => subfolder2
                )

        )

    [9] => Array
        (
            [filename] => subfile4.php
            [path] => Array
                (
                    [0] => 
                    [1] => folder1
                    [2] => folder2
                    [3] => subfolder2
                    [4] => subfolder3
                )

        )

    [10] => Array
        (
            [filename] => subfile5.php
            [path] => Array
                (
                    [0] => 
                    [1] => folder1
                    [2] => folder2
                    [3] => subfolder2
                    [4] => subfolder3
                )

        )

)

Thanks for any idea.

CodePudding user response:

You have already figured out the gist of it. A couple of changes to make this work are as below:

  • Change function multiarrToList($arr, $list = false, $path = false){ to function multiarrToList($arr, $list = [], $path = []){. We will set them as default empty arrays. This helps you completely remove the need for the next if checks.

  • $path[] = $key; is the reason for the duplicacy. This is because for every new subsequent is_array() checks, the previous $key is always included. To rectify this, make a new copy of the array, add the current $key to it and pass this new copy of the array to it's next child calls.

  • $list = multiarrToList($arr[$key],$list,$path); - In this, instead of overwriting $list, I would suggest array_merge() of current $list with the child recursive result.

  • multiarrToList($arr[$key],$list,$path); No need to pass $list again. Just pass an empty array and let it just return only the recursive child hierarchial results for getting it merged at the parent top level.

Snippet:

<?php

function multiarrToList($arr, $list = [], $path = []){
    foreach($arr as $key => $value){
        if(is_array($value)){
            $new_path = $path;
            $new_path[] = $key;
            $list = array_merge($list, multiarrToList($value, [], $new_path));
        } else {
            $list[] = [
                'filename' => $value,
                'path' => $path
            ];
        }

    }
    
    return $list;
}
 

Online Demo

  • Related