title: PHP multidimensional array from database results
body: <p>I'm a bit new to multidimensional arrays, and would like to see if I'm doing it right. preferably, I'd like to name the arrays within the main array for ease of use.</p>
  
  <pre><code>$unique_array = array(
      username=&gt;array(),
      user_id=&gt;array(),
      weeknumber=&gt;array()
      );
  </code></pre>
  
  <p>and then I have a while loop which checks some database results:</p>
  
  <pre><code>while($row = mysql_fetch_array($query)) //yes, I know mysql is deprecated
  {
  $unique_array[] = username=&gt;$row['username'], user_id=&gt;$row['user_id'], week number=&gt;['weeknumber'];
  }
  </code></pre>
  
  <p>I'm not sure if I am placing the values in the array from within the while loop correctly, or if it needs to be done some other way. I couldn't find any resources I could easily understand on SO or elsewhere to deal with query results within a named array within a multidimensional array.</p>
  
  <p>EDIT FOLLOW UP QUESTION: I also need to check the array for duplicate values, because there will be multiple values that are exactly the same, but I only want one of them.</p>
  
  <p>Any help is appreciated!</p>
  
  <p>EDIT SOLUTION:</p>
  
  <p>By modifying the answer I was able to create code to fit my needs.</p>
  
  <p>Array initialization:</p>
  
  <pre><code>$unique_array = array(
      'username'=&gt;array(),
      'user_id'=&gt;array(),
      'weeknumber'=&gt;array()
      );
  </code></pre>
  
  <p>Building the array from within a while loop:</p>
  
  <pre><code>while($row = mysql_fetch_array($query))
  {
  $unique_array[] = array('username'=&gt;$row['username'], 'user_id'=&gt;$row['user_id'], 'weeknumber'=&gt;$row['weeknumber']);
  }
  </code></pre>
  
  <p>And finally, I need to make sure the array values are unique (there are duplicates entries as a result of database and query limitations), after the while loop I have:</p>
  
  <pre><code>print_r(multi_unique($unique_array));
  </code></pre>