title: Generate PHP array from MySQL with key value from "id" column
body: <p>I have a MySQL database that looks like this:</p>
  
  <pre><code>ID        TEXT        PARENTID
  20        Item1        null
  23        Item2        20
  27        Item3        20
  80        Item4        27
  </code></pre>
  
  <p>What I want is to retreive this data in an array like this:</p>
  
  <pre><code>Array ( 
  [20] =&gt; Array ( [text] =&gt; Item1 [parentID] =&gt; ) 
  [23] =&gt; Array ( [text] =&gt; Item2 [parentID] =&gt; 20 ) 
  [27] =&gt; Array ( [text] =&gt; Item3 [parentID] =&gt; 20 ) 
  [80] =&gt; Array ( [text] =&gt; Item4 [parentID] =&gt; 27 )
  );
  </code></pre>
  
  <p>Notice that the key values represent the value of the id column in the table.</p>
  
  <p>I have tried makin an array inside a mysql loop (I am still in the process of learning MySQLi, and hopefully it'll stick pretty soon but this example is in the old MySQL):</p>
  
  <pre><code>$hent_folder = mysql_query("SELECT id, text, parentID FROM folders");   
  while($row = mysql_fetch_assoc($hent_folder))
      {
          $array[] = $row;
      }
  </code></pre>
  
  <p>The result here of course generates "automatic" key values and not the values from the ID column as wanted plus it puts the ID column as a part of the inside array. </p>
  
  <p>Is there a way or workaround to succeed here?</p>
  
  <p>I hope someone can lead me in the right direction... </p>
  
  <p>----- EDIT ------</p>
  
  <p>I have replaced</p>
  
  <pre><code>$array[] = $row;
  </code></pre>
  
  <p>with</p>
  
  <pre><code>$array[$row['ID']] = $row;
  </code></pre>
  
  <p>But it still does not return the above wanted result. It returns this:</p>
  
  <pre><code>Array ( 
  [0] =&gt; Array ( [id] =&gt; 20 [text] =&gt; Item1 [parentID] =&gt; ) 
  [1] =&gt; Array ( [id] =&gt; 23 [text] =&gt; Item2 [parentID] =&gt; 20 ) 
  [2] =&gt; Array ( [id] =&gt; 27 [text] =&gt; Item4 [parentID] =&gt; 20 ) 
  [3] =&gt; Array ( [id] =&gt; 80 [text] =&gt; Item4 [parentID] =&gt; 27 )
  );
  </code></pre>