Tuesday, September 23, 2014

PHP - Reading a file into an array

Quick post on how to read a file into an array. As always there are more than one way to do this.

First create a test.txt file with some lines in it.

linux-test# echo 1st line >> test.txt
linux-test# echo 2nd line >> test.txt
linux-test# echo 3rd line >> test.txt
linux-test# echo 4th line >> test.txt





Next create a test.php

 linux-test# vim test.php  

 <?php  

 //Load the file into an array called $load_file  

 $load_file = file('test.txt');  

 //Create a foreach loop to iterate through the array  

 foreach ($load_file as $lines) {  
  echo "$lines \n";  
 } 
 
 ?>  

Let's run the php file.


Many more articles to come so ....

Please subscribe/comment/+1 if you like my posts as it keeps me motivated to write more and spread the knowledge.

No comments:

Post a Comment