title: converting directory path to javascript object
body: <p>I have a list of directory paths/files in PHP, like so:</p>
  
  <pre><code>path1/path2/foo1_jpg
  path1/foo2_png
  path2/path3/bar_pdf
  path2/
  path1/
  </code></pre>
  
  <p>and I want to convert these to javascript objects. I thought about just converting the "/" to periods and then saying something like:</p>
  
  <pre><code>&lt;script&gt;
      var files = new Array();
      &lt;?php $dirlist = getFileList("images",true); ?&gt; //returns array of directory paths (and other info)
      &lt;?php foreach($dirlist as $file): ?&gt;
          files.&lt;?=$file['name']?&gt; = "$file['name']"; 
  
          //where $file['name'] returns, for example, path1.path2.foo1_jpg
  
      &lt;?php endforeach; ?&gt;
  console.log(files);
  &lt;/script&gt;
  </code></pre>
  
  <p>The ONLY issue I have is if a file name or directory begins with a number, i.e. I can't say: <code>files.001.foo = "bar";</code></p>
  
  <p>I believe that I need to overload the dot operator to handle names beginning with numbers. Is that the best way, or is there another way?</p>
  
  <p>I'm essentially trying to parse a php string of the form <code>"x1x2...xn/y1y2...yn/..."</code>, where x_i,y_i,z_i,etc are characters other than "/", into a nested javascript object <code>x1x2x3...xn.y1y2...yn.z1z2z3...zn = {};</code>. I think I found something of a solution <a href="https://stackoverflow.com/questions/5484673/javascript-how-to-dynamically-create-nested-objects-using-object-names-given-by">here</a> Since this user was also trying to dynamically add javascript objects based on a delimited string (in his case a "_"). </p>