title: Parsing flat-file database information into a multidimensional array
body: <p>I want to make a class for parsing flat-file database information into one large analogous multidimensional array. I had the idea of formatting the database in a sort of python-esque format as follows:</p>
  
  <pre><code>"tree #1":
      "key" "value"
      "sub-tree #1":
          "key" "value"
          "key #2" "value"
          "key #3" "value"
  </code></pre>
  
  <p>I am trying to make it parse this and build and array while parsing it to throw the keys/values into, and I want it to be very dynamic and expandable. I've tried many different techniques and I've been stumped in each of these attempts. This is my most recent:</p>
  
  <pre><code>function parse($file=null) {
      $file = $file ? $file : $this-&gt;dbfile;
  
      ### character variables
  
      # get values of 
      $src = file_get_contents($file);
      # current character number
      $p = 0;
  
      ### array variables
  
      # temp shit
      $a = array();
      # set $ln keys
      $ln = array("q"=&gt;0,"k"=&gt;null,"v"=&gt;null,"s"=&gt;null,"p"=&gt;null);
      # indent level
      $ilvl = 0;
  
      ### go time
  
      while (strlen($src) &gt; $p) {
          $chr = $src[$p];
          # quote
          if ($chr == "\"") {
              if ($ln["q"] == 1) { // quote open?
                  $ln["q"] = 0; // close it
                  if (!$ln["k"]) { // key yet?
                      $ln["k"] = $ln["s"]; // set key
                      $ln["s"] = null;
                      $a[$ln["k"]] = $ln["v"]; // write to current array
                  } else { // value time
                      $ln["v"] = $ln["s"]; // set value
                      $ln["s"] = null;
                  }
              } else {
                  $ln["q"] = 1; // open quote
              }
          }
  
          elseif ($chr == "\n" &amp;&amp; $ln["q"] == 0) {
              $ln = array("q"=&gt;0,"k"=&gt;null,"v"=&gt;null,"s"=&gt;null,"p"=&gt;null);
              $llvl = $ilvl;
  
          }
          # beginning of subset
          elseif ($chr == ":" &amp;&amp; $ln["q"] == 0) {
              $ilvl++;
              if (!array_key_exists($ilvl,$a)) { $a[$ilvl] = array(); }
              $a[$ilvl][$ln["k"]] = array("@mbdb-parent"=&gt; $ilvl-1 .":".$ln["k"]);
              $ln = array("q"=&gt;0,"k"=&gt;null,"v"=&gt;null,"s"=&gt;null,"p"=&gt;null);
              $this-&gt;debug("INDENT++",$ilvl);
          }
          # end of subset
          elseif ($chr == "}") {
              $ilvl--;
              $this-&gt;debug("INDENT--",$ilvl);
          }
          # other characters
          else {
              if ($ln["q"] == 1) {
                  $ln["s"] .= $chr;
              } else {
                  # error
              }
          }
          $p++;
      }
      var_dump($a);
  }
  </code></pre>
  
  <p>I honestly have no idea where to go from here. The thing troubling me most is setting the multidimensional values like <code>$this-&gt;c["main"]["sub"]["etc"]</code> the way I have it here. Can it even be done? How can I actually nest the arrays as the data is nested in the db file?</p>