title: HTML table in php foreach loop where the cell data is only populated if an element is equal to the table column head name
body: <p>I have created my column headers in my table by using:</p>
  <pre><code>if (count($round) &gt; 0): ?&gt;
  &lt;table&gt;
    &lt;thead&gt;
      &lt;tr&gt;
  &lt;?php foreach ($round as $colhead): array_map('htmlentities', $colhead); ?&gt;
        &lt;th&gt;&lt;?php  echo implode('&lt;/th&gt;&lt;th&gt;',$colhead); ?&gt;&lt;/th&gt;
      &lt;/tr&gt;
  &lt;?php endforeach; endif;
  ?&gt;
    &lt;/thead&gt;
  </code></pre>
  <p><code>$round</code> is:</p>
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [32] =&gt; -7
              [31] =&gt; -6
              [29] =&gt; -5
              [27] =&gt; -4
              [23] =&gt; -3
              [19] =&gt; -2
              [17] =&gt; -1
              [0] =&gt; 1
              [2] =&gt; 2
              [10] =&gt; 3
              [14] =&gt; 4
              [16] =&gt; 5
              [33] =&gt; 6
          )
  
  )
  </code></pre>
  <p>This results in exactly what I want because <code>$round</code> won't always result in the same number of columns.</p>
  <p>The data I want in the table row beneath those headers needs to be only those items that contain the column header's name.</p>
  <p>Let's say the column headers are:
  <strong>|  -7  |  -6  |  -5  |  -4  |  -3  |  -2  |  -1  |  1  |  2  |  3  |  4  |  5  |  6  |</strong>
  based on the <code>foreach</code> loop above.</p>
  <p>I used the following to then populate my first column in the next row:</p>
  <pre><code>&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td&gt;
    &lt;?php foreach ($matches as $match)  {
  $firstCharacter = $match['match']['scores_csv'][0];
  $lastCharacter = $match['match']['scores_csv'][-1]; 
  $id1 = $match['match']['player1_id'];
  $id2 = $match['match']['player2_id'];
      if ($match['match']['round'] === -7) 
        echo  &quot;Table: &quot;.$match['match']['player1_votes'].&quot;&lt;/br&gt;&quot;.$participants[$id1][&quot;name&quot;].$participants[$id1]['misc'].&quot;: &quot;.$firstCharacter. &quot;&lt;/br&gt;&quot;.$participants[$id2][&quot;name&quot;].$participants[$id2]['misc'].&quot;:&quot;.$lastCharacter. &quot;&lt;/br&gt;&quot;;
    }
    ?&gt;
      &lt;/td&gt;
     &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  <p>And while it works, it's only because I know the header of my first column in the table is -7.  I'm trying to figure out how to only display those items that are equal to my column header to display in the following row based on the column header so that I can then do another foreach loop to display the remainder of my table.  So basically, instead of -7 that I have above, what do I need to do?  I'm completely drawing a blank.</p>
  <p>$match['match']['round'] is what is equal to the column headers.  And a small sample $matches is below in case that helps.</p>
  <pre><code> [{&quot;match&quot;:{&quot;id&quot;:246811883,&quot;tournament_id&quot;:10128202,&quot;state&quot;:&quot;complete&quot;,&quot;player1_id&quot;:152239163,&quot;player2_id&quot;:152239166,&quot;player1_prereq_match_id&quot;:null,&quot;player2_prereq_match_id&quot;:null,&quot;player1_is_prereq_match_loser&quot;:false,&quot;player2_is_prereq_match_loser&quot;:false,&quot;winner_id&quot;:152239166,&quot;loser_id&quot;:152239163,&quot;started_at&quot;:&quot;2021-08-07T01: 59: 51.967+02: 00&quot;,&quot;created_at&quot;:&quot;2021-08-07T01: 59: 51.692+02: 00&quot;,&quot;updated_at&quot;:&quot;2021-08-07T02: 22: 29.417+02: 00&quot;,&quot;identifier&quot;:&quot;A&quot;,&quot;has_attachment&quot;:false,&quot;round&quot;:1,&quot;player1_votes&quot;:1,&quot;player2_votes&quot;:null,&quot;group_id&quot;:null,&quot;attachment_count&quot;:null,&quot;scheduled_time&quot;:null,&quot;location&quot;:null,&quot;underway_at&quot;:&quot;2021-08-07T02: 05: 31.834+02: 00&quot;,&quot;optional&quot;:false,&quot;rushb_id&quot;:null,&quot;completed_at&quot;:&quot;2021-08-07T02: 22: 29.455+02: 00&quot;,&quot;suggested_play_order&quot;:null,&quot;forfeited&quot;:null,&quot;open_graph_image_file_name&quot;:null,&quot;open_graph_image_content_type&quot;:null,&quot;open_graph_image_file_size&quot;:null,&quot;prerequisite_match_ids_csv&quot;:&quot;&quot;,&quot;scores_csv&quot;:&quot;0-2&quot;}},{&quot;match&quot;:{&quot;id&quot;:246811884,&quot;tournament_id&quot;:10128202,&quot;state&quot;:&quot;complete&quot;,&quot;player1_id&quot;:152239594,&quot;player2_id&quot;:152239168,&quot;player1_prereq_match_id&quot;:null,&quot;player2_prereq_match_id&quot;:null,&quot;player1_is_prereq_match_loser&quot;:false,&quot;player2_is_prereq_match_loser&quot;:false,&quot;winner_id&quot;:152239594,&quot;loser_id&quot;:152239168,&quot;started_at&quot;:&quot;2021-08-07T01: 59: 51.984+02: 00&quot;,&quot;created_at&quot;:&quot;2021-08-07T01: 59: 51.698+02: 00&quot;,&quot;updated_at&quot;:&quot;2021-08-07T02: 32: 30.655+02: 00&quot;,&quot;identifier&quot;:&quot;B&quot;,&quot;has_attachment&quot;:false,&quot;round&quot;:1,&quot;player1_votes&quot;:2,&quot;player2_votes&quot;:null,&quot;group_id&quot;:null,&quot;attachment_count&quot;:null,&quot;scheduled_time&quot;:null,&quot;location&quot;:null,&quot;underway_at&quot;:&quot;2021-08-07T02: 05: 23.667+02: 00&quot;,&quot;optional&quot;:false,&quot;rushb_id&quot;:null,&quot;completed_at&quot;:&quot;2021-08-07T02: 32: 30.690+02: 00&quot;,&quot;suggested_play_order&quot;:null,&quot;forfeited&quot;:null,&quot;open_graph_image_file_name&quot;:null,&quot;open_graph_image_content_type&quot;:null,&quot;open_graph_image_file_size&quot;:null,&quot;prerequisite_match_ids_csv&quot;:&quot;&quot;,&quot;scores_csv&quot;:&quot;2-1&quot;}}]
  </code></pre>
  <p>I've attempted to reindex the array by using:</p>
  <pre><code>$matchesa = array_column($matches, 'match');
  $matchesa = array_column($matchesa, null, 'round');
  
  foreach ( $matches as $match )  {
              $id1 = $match['match']['round'];
   {
          echo $matchesa[$id1]['round'].PHP_EOL;
      }
    }
  </code></pre>
  <p>Desired output is like this:
  <a href="https://i.stack.imgur.com/Pxcz3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Pxcz3.png" alt="enter image description here" /></a></p>