#!/usr/bin/php $value) { $dir[$key] = array_flip(scandir($path.'/'.$key, 0)); unset($dir[$key]['.']); unset($dir[$key]['..']); recursive_scandir($path.'/'.$key,$dir[$key]); } } $dds = array_flip(array('dds')); recursive_scandir('./', $dds); //print_r($dds); //you can use this if you don't have JSON ext. echo json_encode($dds);