samedi 23 février 2019

Foreach in multidimensional array doesnt work

I have two methods for looking into directories and making a tree:

public function getFolders($path) {
        $folders = \Storage::disk('public')->directories($path);
        return count($folders) > 0 ? $folders : [] ;
    }

public function getTree($path = '') {
    if(count($tree = $this->getFolders($path)) === 0) {
        return ['nema'];
    }

    $arr = [];
    foreach($tree as $value) {
        $folder = str_replace($path, '', $value);
        if( count( explode('/', $folder) ) === 1 ) {
            if(count($this->getFolders($value)) > 0) {
                $arr[$folder] = $this->getTree($value . '/');
            }else{
                $arr[$folder] = $folder;
            }
        }
    }
    return $arr;
}

When I dd($arr) I get:

array:1 [▼
  "files" => array:2 [▼
    422 => array:1 [▶]
    "shares" => array:2 [▼
      "sasic" => array:2 [▼
        "daasdasd" => "daasdasd"
        "thumbs" => "thumbs"
      ]
      "thumbs" => "thumbs"
    ]
  ]
]

But when I try this:

foreach($arr as $folder){
    $test = 0;
    if(is_array($folder)){
        $test = $test + 1;
    }
}

return $test; // returns 0 like there is zero arrays

And very strange thing is that if I use echo in foreach, it doesnt go from top to bottom but on some mixed order.

Any ideas?

I am trying to make menu tree from only directories within some folder in Laravel storage public disk.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire