title: how to encode this data to parent / children structure in JSON
body: <p>I am working with d3.js to visualise families of animals (organisms) (up to 4000 at a time) as a tree graph, though the data source could just as well be a directory listing, or list of namespaced objects. my data looks like:</p>
  
  <pre><code>json = {
      organisms:[
          {name: 'Hemiptera.Miridae.Kanakamiris'},
          {name: 'Hemiptera.Miridae.Neophloeobia.incisa'},
          {name: 'Lepidoptera.Nymphalidae.Ephinephile.rawnsleyi'},
          ... etc ...
      ]
  }
  </code></pre>
  
  <p>my question is: I am trying to find the best way to convert the above data to the hierarchical parent / children data structure as is used by a number of the d3 visualisations such as <a href="http://bost.ocks.org/mike/treemap/" rel="noreferrer">treemap</a> (for data example see <a href="https://github.com/mbostock/d3/blob/master/examples/data/flare.json" rel="noreferrer">flare.json</a> in the d3/examples/data/ directory).
  Here is an example of the desired data structure:</p>
  
  <pre><code>{"name": "ROOT",
   "children": [
          {"name": "Hemiptera",
           "children": [
               {"name": "Miridae",
                "children": [
                    {"name": "Kanakamiris", "children":[]},
                    {"name": "Neophloeobia",
                     "children": [
                         {"name": "incisa", "children":[] }
                     ]}
                ]}
           ]},
          {"name": "Lepidoptera",
           "children": [
               {"name": "Nymphalidae",
                "children": [
                    {"name": "Ephinephile",
                     "children": [
                         {"name": "rawnsleyi", "children":[] }
                     ]}
                ]}
           ]}
      ]}
  }
  </code></pre>
  
  <p>EDIT: enclosed all the original desired data structure inside a <code>ROOT</code> node, so as to conform with the structure of the d3 examples, which have only one master parent node.</p>
  
  <p>I am looking to understand a general design pattern, and as a bonus I would love to see some  solutions in either javascript, php, (or even python). javascript is my preference.
  In regards to php: the data I am actually using comes from a call to a database by a php script that encodes the results as json.
  database results in the php script is an ordered array (see below) if that is any use for php based answers.</p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              ['Rank_Order'] =&gt; 'Hemiptera'
              ['Rank_Family'] =&gt; 'Miridae'
              ['Rank_Genus'] =&gt; 'Kanakamiris'
              ['Rank_Species'] =&gt; ''
          ) ........
  </code></pre>
  
  <p>where:
  <code>'Rank_Order'</code> isParentOf <code>'Rank_Family'</code> isParentOf <code>'Rank_Genus'</code> isParentOf <code>'Rank_Species'</code></p>
  
  <p>I asked a similar question focussed on a php solution <a href="https://stackoverflow.com/questions/11981153/rearrange-a-php-array-into-a-nested-hierarchical-array">here</a>, but the only answer is not working on my server, and I dont quite understand what is going on, so I want to ask this question from a design pattern perspective, and to include reference to my actual use which is in javascript and d3.js.</p>