title: Why is my PHP foreach loop not correctly outputting data into HTML Table?
body: <p>I am outputting data from a text file (not csv) into an html table using php. I am exploding the data and it returns as an array but when I use a foreach loop the data turns back into a string so I am unable to sort(). Also, the data is printing into the table 5 times as a duplicate for each book and author. How do I keep my array as an array in the foreach loop so it can be sorted and how do I only get each line to display one time in the html table? (to be properly looped through to get the display I am looking for?).</p>
  
  <p>PHP</p>
  
  <pre><code>&lt;?php
  $filename = 'books.txt';
  list($rows, $html_table ) = returnTable($filename);
  
  if (empty($rows))
  {
      print "&lt;p&gt;Total Rows in Table: ".$rows."&lt;/p&gt;";
  
      print $html_table;
  }
  else
  {
      print "No file found";
  }
  
  
  //Read Information from a File into an HTML table
  
  function returnTable($filename)
  {
  
      $arr = array();
  
      $html_table = "&lt;table border='2'&gt;";
             $html_table.= "&lt;tr&gt;";
             $html_table.= "&lt;th&gt;Title&lt;/th&gt;";
             $html_table.= "&lt;th&gt;Author&lt;/th&gt;";
             $html_table.= "&lt;/tr&gt;\n";
  
      $line_ctr = 0;
  
  
      $fp = fopen($filename, 'r'); 
  
      if ($fp)
      {
          while(true)
          {
  
              $line = fgets($fp);
  
              if (feof($fp))
              {
                  break;
              }
  
              $line_ctr++;
  
              $arr = list($title, $author) = explode('*', $line);
  
              //var_dump($arr); // returns an array but changes to a string in the loop below!
  
              foreach($arr as $books){
                  $html_table.= "&lt;tr&gt;";
                  $html_table.= "&lt;td&gt;".$title."&lt;/td&gt;";
                  $html_table.= "&lt;td&gt;".$author."&lt;/td&gt;";
                  $html_table.= "&lt;/tr&gt;\n";
              }//END OF FOREACH LOOP           
  
          } //END OF WHILE LOOP
  
          $html_table.= "&lt;/table&gt;";
  
          fclose($fp ); //Close file
  
          //AT THIS POINT html_table is a string again but why?
          $return_data = array($line_ctr, $html_table);
  
      } else {
          $return_data = array("error", "No Content here");
      }
  
      return $return_data;
  }
  ?&gt;
  </code></pre>