Create a Multidimensional Array with PHP and MySQL
<p>I am new to PHP and am looking at efficient ways to return data from the database. Lets say I have a UserProfile table that has a one to many relationship with UserInterest and UserContact:</p>

<pre><code>Select p.Id, p.FirstName, p.LastName, i.Name as Interests, c.Email, c.Phone
from UserProfile p
left join UserInterest i on p.Id = i.UserProfileId
left join UserContact c on p.Id = c.UserProfileId
where p.Id = 1
</code></pre>

<p>An efficient way to retrieve data  would be to create a multidimensional array such as:</p>

<pre><code>$user = array(  "FirstName" =&gt; "John", 
                "LastName" =&gt; "Doe", 
                "Gender" =&gt; "Male", 
                "Interests" =&gt; array(
                    "Hiking", 
                    "Cooking"), 
                "Contact" =&gt; array(
                    "Email" =&gt; "john.doe@gmail.com", 
                    "Phone" =&gt; "(555) 555-5555"));
</code></pre>

<p>I can't seem to get my head around how this would be constructed in PHP.  For simple data like interests I could use group_concat(i.Name) as Interests in the query to return interests back as a comma separated list in a single row, however, for an associative array such as Contact, I'd like to be able to get a handle on each key in the array using $user['Contact']['Email']. </p>

<p>From a "Best Practices" standpoint, I would assume that constructing an array like this in one query is a lot better than hitting the database multiple times to retrieve this data.</p>

<p>Thanks!</p>

<p>Neil</p>
