Remove the unwanted array levels

After created my file tree, I have got this result which is shown as below :

Here,In This image, the orange boxes shows the unwanted array for only directories, except files.

The function which has been created for now as shown below:

public function addresourceTree(Resource $r) {
        $currentNode = &$this->filesTree;
        $exploded = explode('/', $r->getPath());

        if (! $r->getPath()) {
            return;
        }

        if ($r->isDirectory()) {
            $lastIndex = count($exploded) - 2;
            $resourceName = $exploded[$lastIndex];
        } else {
            $lastIndex = count($exploded) - 1;
            $resourceName = $exploded[$lastIndex];
        }

        for ($index = 1; $index < $lastIndex ; $index++) {
            $cur = $exploded[$index];
            $currentNode = &$currentNode[$cur]['children'];
        }

        $resourceDescr = [];
        $resourceDescr['fpath'] = $r->getPath();            

        if ($r->isDirectory()) {
            $resourceDescr['children'] = [];
        }
        
        $currentNode[$resourceName] = $resourceDescr;
    }

How to remove that unwanted level for clear file tree structure ?