title: PHP looping through huge text file is very slow, can you improve?
body: <p>The data contained in the text file (actually a .dat) looks like:</p>
  
  <pre><code>LIN*1234*UP*abcde*33*0*EA
  LIN*5678*UP*fghij*33*0*EA
  LIN*9101*UP*klmno*33*23*EA
  </code></pre>
  
  <p>There are actually over 500,000 such lines in the file.</p>
  
  <p>This is what I'm using now:</p>
  
  <pre><code>//retrieve file once        
  $file = file_get_contents('/data.dat'); 
  $file = explode('LIN', $file);
  
      ...some code
  
  foreach ($list as $item) { //an array containing 10 items
       foreach($file as $line) { //checking if these items are on huge list
           $info = explode('*', $line);
           if ($line[3] == $item[0]) {
               ...do stuff...                     
               break; //stop checking if found
            }
        }         
   }
  </code></pre>
  
  <p>The problem is it runs way too slow - about 1.5 seconds of each iteration. I separately confirmed that it is not the '...do stuff...' that is impacting speed. Rather, its the search for the correct item.</p>
  
  <p>How can I speed this up? Thank you.</p>