Parse Directory Structure (Strings) to JSON using PHP

Posted by Ecropolis on Stack Overflow See other posts from Stack Overflow or by Ecropolis
Published on 2011-02-13T23:10:06Z Indexed on 2011/02/13 23:25 UTC
Read the original article Hit count: 346

I have an array of file-path strings like this

  • videos/funny/jelloman.wmv
  • videos/funny/bellydance.flv
  • videos/abc.mp4
  • videos/june.mp4
  • videos/cleaver.mp4
  • fun.wmv
  • jimmy.wmv
  • herman.wmv

Is there a library or easy way I can get to a data structure json or xml? Something like this: (I see there are a lot of snippets available for traversing actual folders, but again, I just have strings.)

{  
files:{  
        file:[    
            {    
                filename:'fun.wmv'  
            },  
            {  
                filename:'jimmy.wmv'  
            },  
            {  
                filename:'herman.wmv'  
            }  
        ],  
        folder:{  
            foldername:'videos',  
            file:[  
                {  
                    filename:'abc.mp4'  
                },  
                {  
                    filename:'june.mp4'  
                },  
                {  
                    filename:'cleaver.mp4'  
                }  
            ],  
            folder:{  
                foldername:'funny',  
                file:[  
                    {  
                        filename:'jelloman.wmv'  
                    },  
                    {  
                        filename:'bellydance.flv'  
                    }  
                ]  
            }  
        }  
    }  
} 

© Stack Overflow or respective owner

Related posts about php

Related posts about JSON