title: Making a JSON tree from a flat PHP SQL table
body: <p>What would be the cleanest and most efficient way to create a JSON tree representing a table from a MySQL query involving multiple JOINS ?</p>
  
  <p><img src="https://i.stack.imgur.com/m6NUp.png" alt="enter image description here"></p>
  
  <p>So far, the php array is created by this loop:</p>
  
  <pre><code>$rs = mysqli_query($connect, $query);
  $arr = array();
  while ($row = mysqli_fetch_array($rs, MYSQL_ASSOC)) {
      $arr[] = $row;
  }
  </code></pre>
  
  <p>I could then do echo $arr[2]["sold"] and get "2 sodas"</p>