I have a PHP function to create a directory tree and I cannot format the result to obtain a JSON file like this:

```
[
  {
    text: 'Parent',
    href: 'parent/',
    nodes: [
      {
        text: 'Child',
        href: 'child/',
        nodes: [
          {
            text: 'Grandchild',
            href: 'grandchild/',
          },
          {
            text: 'Grandchild',
            href: 'grandchild/',
          }
        ]
      },
      {
        text: 'Child',
        href: 'child/',
      }
    ]
  },
  {
    text: 'Parent',
    href: 'parent/',
  },
  {
    text: 'Parent',
    href: 'parent/',
  },
  {
    text: 'Parent',
    href: 'parent/',
  },
  {
    text: 'Parent',
    href: 'parent/',
  },
  {
    text: 'Parent',
    href: 'parent/',
    nodes: [
      {
        text: 'Child',
        href: 'child/',
        nodes: [
          {
            text: 'Grandchild',
            href: 'grandchild/',
          },
          {
            text: 'Grandchild',
            href: 'grandchild/',
          }
        ]
      },
      {
        text: 'Child',
        href: 'child/',
      }
    ]
  }
]

```

Here my PHP function, someone can help me? Thanks

```
function scandir_rec($root)
{
    $data = [];

    if (!is_dir($root)) {
        return;
    }

    $dirs = scandir($root);

    foreach ($dirs as $dir) {

        if ($dir == '.' || $dir == '..') {
            continue;
        }

        $path = $root . '/' . $dir;  

        $data[] = ['text'=>$dir, 'link'=>urlencode($path)];

        if (is_dir($path)) {

            $data[] = ['nodes' => scandir_rec($path)]; 

        }
    }

    return json_encode($data, JSON_UNESCAPED_SLASHES);
}

// init call
$rootDir = '/var/usr';

scandir_rec($rootDir);

```