title: php dynamically create multi dimensional array
body: <p>I am writing a php script to extract some data from a MYSQL db, then I want to take that data and store it into a two dimensional array. Thinking of it normally, if I use code like this</p>
  
  <pre><code>$test = array();
  $test[0]=array();
  $test[0]['hello']='hello';
  $test[1]=array();
  $test[1]['hello']='hello';
  $test[2]=array();
  $test[2]['hello']='hello';
  print_r($test);
  </code></pre>
  
  <p>the output will be:</p>
  
  <pre><code>Array ( [0] =&gt; Array ( [hello] =&gt; hello ) [1] =&gt; Array ( [hello] =&gt; hello ) [2] =&gt;
  Array ( [hello] =&gt; hello ) )
  </code></pre>
  
  <p>which is how I want my output to be</p>
  
  <p>so this is what I do in my script</p>
  
  <p>So basically in my database I have table with standings for a women's league and the columns are</p>
  
  <pre><code>team_name, played, won, drawn, lost, for, against, points
  </code></pre>
  
  <p>All the connections have been taken care of successfully, below is my query</p>
  
  <pre><code>$get_ladies_query = "SELECT 
  `team_name`, `played`, `won`, `drawn`, `lost`, `for`, `against`, `points` 
  FROM `standings_ladies` order by pos";
  </code></pre>
  
  <p>An important point to not before I show the next code is that, there are 2 other standings tables, men_senior and men_intermediate with the same structure but obviously only the data changes, below are the two queries just incase</p>
  
  <pre><code>$get_mens_inter_query = "SELECT 
  `team_name`, `played`, `won`, `drawn`, `lost`, `for`, `against`, `points` 
  FROM `standings_men_inter` order by pos";
  
      $get_mens_senior_query = "SELECT 
  `team_name`, `played`, `won`, `drawn`, `lost`, `for`, `against`, `points` 
  FROM `standings_men_senior` order by pos";
  </code></pre>
  
  <p>Now I create 3 arrays which I want to hold standings seperately for the ladies, mens senior, mens intermediate</p>
  
  <pre><code>$standings_ladies = array();
  $standings_men_inter = array();
  $standings_men_senior = array();
  </code></pre>
  
  <p>The data I want displayed in the array is like so</p>
  
  <pre><code>array(0=&gt;array(team_name,wins,drawn,lost,for,against,points)
  1=&gt;array(team_name,wins,drawn,lost,for,against,points)) and so on
  </code></pre>
  
  <p>Now since I wanted to create the multidimensional arrays of standings for all 3 categories, I could have run the queries in 3 separate while loops altough I thought, I could accomplish the same result in 1 and i felt it would help improve performance. If its better to use 3 while loops, please let me know, what I tried is below.</p>
  
  <pre><code>//I run the 3 queries and store them in the given variables
  $result_mens_senior = mysqli_query($link,$get_mens_senior_query);
  $result_mens_inter = mysqli_query($link,$get_mens_inter_query);
  $result_ladies= mysqli_query($link, $get_ladies_query);
  
  //I want to create 1 while loop so based of the results returned from the 3 
  //queries so based on the results returned from the 3 queries, 
  //I get the max number of times I want the query to run
  $ladies_boundary = mysqli_num_rows($result_ladies);
  $mens_senior_boundary = mysqli_num_rows($result_mens_senior);
  $mens_inter_boundary = mysqli_num_rows($result_mens_inter);
  $max_size = max(array($ladies_boundary,$mens_senior_boundary,$mens_inter_boundary));
  
  //set an index to start from 0
  $index = 0;
  
  //I will only show the example for 1 of the arrays but you get an idea
  that this issue will occur for all
  while ($index &lt; $max_size)
  {
  //first, everytime the loop is entered, we need the next row to be fetched
  
      $standings_men_inter_table = mysqli_fetch_assoc($result_mens_inter);
      $standings_ladies_table = mysqli_fetch_assoc($result_ladies);
  
  //there is a high chance that the other two tables return a different row size
  //so its best to check that we do not go beyond
  if($index &lt; $mens_senior_boundary)
      {
              //we fetch the rows every time we enter the block
              $standings_men_senior_table = mysqli_fetch_assoc($result_mens_senior);
  
              //then, this is how I attempt at creating the 2 dimensional array
          array_push($standings_men_senior, array(
          $standings_men_senior_table['team_name'],
          $standings_men_senior_table['played'],
          $standings_men_senior_table['won'],
          $standings_men_senior_table['drawn'],
          $standings_men_senior_table['lost'],
          $standings_men_senior_table['for'],
          $standings_men_senior_table['against'],
          $standings_men_senior_table['points']));
      }
  
  //incrementing index each time the loop runs
  $index++;
  
  }
  </code></pre>
  
  <p>Then finally, I just want to print what I think is the array but get this, attached image, hope you can see it clearly
  <img src="https://i.stack.imgur.com/bmoFv.jpg" alt="enter image description here"></p>
  
  <p>Just to investigate even further, every time, the 'if' block is entered, I just commented everything out and just put this to see what was being returned</p>
  
  <pre><code>if($index &lt; $mens_senior_boundary)
  {
      print_r(mysqli_fetch_assoc($result_mens_senior));
  }
  </code></pre>
  
  <p>The output I got was almost 90% what I need</p>
  
  <pre><code>Array 
  ([team_name] =&gt; Morley Gaels [played] =&gt; 8 [won] =&gt; 6 [drawn] =&gt; 2 
  [lost] =&gt; 0 [for] =&gt; 110 [against] =&gt; 83 [points] =&gt; 14 ) 
  Array ( [team_name] =&gt; Southern Districts [played] =&gt; 8 [won] =&gt; 3 [drawn] =&gt; 2 
  [lost] =&gt; 3 [for] =&gt; 104 [against] =&gt; 98 [points] =&gt; 8 ) 
  Array ( [team_name] =&gt; St Finbarrs [played] =&gt; 8 [won] =&gt; 3 [drawn] =&gt; 2 
  [lost] =&gt; 3 [for] =&gt; 107 [against] =&gt; 99 [points] =&gt; 8 ) 
  Array ( [team_name] =&gt; Western Shamrocks [played] =&gt; 8 [won] =&gt; 3 [drawn] =&gt; 0 
  [lost] =&gt; 5 [for] =&gt; 96 [against] =&gt; 88 [points] =&gt; 6 ) 
  Array ( [team_name] =&gt; Greenwood [played] =&gt; 8 [won] =&gt; 1 [drawn] =&gt; 1 
  [lost] =&gt; 9 [for] =&gt; 82 [against] =&gt; 109 [points] =&gt; 3 )
  </code></pre>
  
  <p>What I need is for example:</p>
  
  <pre><code>Array(0=&gt;Array 
  ([team_name] =&gt; Morley Gaels [played] =&gt; 8 [won] =&gt; 6 [drawn] =&gt; 2 
  [lost] =&gt; 0 [for] =&gt; 110 [against] =&gt; 83 [points] =&gt; 14 )
  1=&gt;Array
  ([team_name] =&gt; Southern Districts [played] =&gt; 8 [won] =&gt; 3 [drawn] =&gt; 2 
  [lost] =&gt; 3 [for] =&gt; 104 [against] =&gt; 98 [points] =&gt; 8 )..... so on);
  </code></pre>
  
  <p>My questions are</p>
  
  <ul>
  <li>What is wrong with my code and what is the correct way to dynamically create
  multidimensional arrays in php ?
  
  <ul>
  <li>Is there something I have not understood about how mysql_fetch_assoc works and how it returns ?</li>
  <li>Anything to improve, anything I am doing wrong ?</li>
  </ul></li>
  </ul>
  
  <p>I appreciate your time, than you for reading it, I tried to be as detailed as I can about what I have tried. </p>
  
  <p>Thank You.</p>