title: Why insert some unwanted values in database
body: <p>after I wrote the following php script:</p>
  
  <pre><code>&lt;?php
  
  include_once ('C:\Users\fabio.gatta\OneDrive - Accenture\MySite\moduli\simple_html_dom.php');
  
  $url = 'https://www.sports-reference.com/olympics/summer/1896/ATH/';
  $tagname_h1 = 'h1';
  $tagname_tr = 'tr  class=""';
  
  
      $olympiad = array();
      $html = file_get_html($url,true);
      foreach($html-&gt;find($tagname_h1) as $tag) {
          $olympiad[] = trim($tag-&gt;innertext);
      }
  
  
      $result_oly=explode(" ",$olympiad[0]);
  
      $year = $result_oly[3];
      $city = $result_oly[4];
      $season = $result_oly[5];
      $sport = $result_oly[0];
  
  //Prendo le informazioni dei vincitori
  
      $athletes = array();
      $country = array();
  
      foreach($html-&gt;find($tagname_tr) as $tag)
      {
          foreach($tag-&gt;find('td') as $td)
          {
              $a_tags = $td-&gt;find('a');
             $img_tags = $td-&gt;find('img');
  
              if(count($a_tags) ==0)
              {
                  $ath ="";
              }
              elseif(count($a_tags)==1)
              {               
                  $ath = $a_tags[0]-&gt;innertext;
  
  
              }
              else
              {
                  $ath = array();
                  foreach($a_tags as $a)
                  {
                      $ath[] = $a-&gt;innertext;
  
                  }
              }
              $athletes[] = $ath;
  
              if(count($img_tags) ==0)
              {
                  $coun ="";
              }
              elseif(count($img_tags)==1)
              {               
                  $coun = $img_tags[0]-&gt;alt;
  
              }
              else
              {
                  $coun = array();
                  foreach($img_tags as $alt)
                  {
                      $coun[] = $alt-&gt;alt;
  
                  }
              }
  
              $country[] = $coun;         
          }
  
          $medals = ["gold", "silver", "bronze"];
  
  foreach ($athletes as $key =&gt; $values) {
  
      if (strpos($values,"'s ") &gt; 0){
  
          $gender="";
          $event="";
  
          list($gender,$event)=explode("'s ",$values);
      }   
  
             //connessione al database
  
         $link = mysqli_connect('192.168.0.97', 'root', 'admin','Olympics');
          if (!$link) {
          die('Could not connect: ' . mysqli_error());
           }
              echo 'Connected successfully'. '&lt;br&gt;';
  
       //prendo gli ori
        if ($key == 1){
  
          if (count($values) == 1){
  
           $ath_key = $athletes[$key];
           $country_key = $country[$key];
           $medals_key = $medals[$key-1];
  
           mysqli_query($link,"INSERT INTO MedalResults (Year,City,Season,Sport,Event,Athlete,Gender,Country,Medal) VALUES ('$year', '$city', '$season', '$sport', '$event', '$ath_key',' $gender', '$country_key', '$medals_key')");
  
          }
  
          elseif (count($values) &gt; 1){
  
              foreach($values as $sub_key =&gt; $sub_values){
  
                  $ath_key = $athletes[$sub_key+1];
                  $country_key = $country[$sub_key+1];
                  $medals_key = $medals[$key-1];
  
                mysqli_query($link,"INSERT INTO MedalResults (Year,City,Season,Sport,Event,Athlete,Gender,Country,Medal) VALUES ('$year', '$city', '$season', '$sport', '$event', '$ath_key',' $gender', '$country_key', '$medals_key')");
  
          }
          }
         }
  
         //prendo gli argenti
  
         if ($key == 2){
  
          if (count($values) == 1){
  
            $ath_key = $athletes[$key];
           $country_key = $country[$key];
           $medals_key = $medals[$key-1];     
  
           mysqli_query($link,"INSERT INTO MedalResults (Year,City,Season,Sport,Event,Athlete,Gender,Country,Medal) VALUES ('$year', '$city', '$season', '$sport', '$event', '$ath_key',' $gender', '$country_key', '$medals_key')");
  
          }
  
          elseif (count($values) &gt; 1){
  
              foreach($values as $sub_key =&gt; $sub_values){
  
               $ath_key = $athletes[$key];
              $country_key = $country[$key];
              $medals_key = $medals[$key-1];
  
  
               mysqli_query($link,"INSERT INTO MedalResults (Year,City,Season,Sport,Event,Athlete,Gender,Country,Medal) VALUES ('$year', '$city', '$season', '$sport', '$event', '$ath_key',' $gender', '$country_key', '$medals_key')");
          }
          }
         }
  
         //prendo i bronzi
         if ($key == 3){
  
          if (count($values) == 1){        
  
             $ath_key = $athletes[$key];
              $country_key = $country[$key];
              $medals_key = $medals[$key-1];
  
             mysqli_query($link,"INSERT INTO MedalResults (Year,City,Season,Sport,Event,Athlete,Gender,Country,Medal) VALUES ('$year', '$city', '$season', '$sport', '$event', '$ath_key',' $gender', '$country_key', '$medals_key')");
  
          }
  
          elseif (count($values) &gt; 1){
  
              foreach($values as $sub_key =&gt; $sub_values){
  
               $ath_key = $athletes[$key];
              $country_key = $country[$key];
              $medals_key = $medals[$key-1];
  
               mysqli_query($link,"INSERT INTO MedalResults (Year,City,Season,Sport,Event,Athlete,Gender,Country,Medal) VALUES ('$year', '$city', '$season', '$sport', '$event', '$ath_key',' $gender', '$country_key', '$medals_key')");
  
              }
          }
         }
  
          mysqli_close($link);
    }
  
     unset($athletes);
     unset($country);
  
      }   
  
  ?&gt;
  </code></pre>
  
  <p>In the database I have some unwanted rows like:</p>
  
  <pre><code>1896 Athina Summer Athletics 100 metres Tom Burke Men USA gold
  1896 Athina Summer Athletics 100 metres Fritz Hofmann Men GER silver
  1896 Athina Summer Athletics 100 metres Array Men Array bronze
  1896 Athina Summer Athletics 100 metres Array Men Array bronze
  </code></pre>
  
  <p>Why do I get Array instead of athlete name and country name?</p>
  
  <p>Second issue: some of the athlete and country array elements are null and thanks to if statement I think to exclude them, but I get still rows with athlete and country null. How avoid this?</p>