Q:

php - Display first item from each line in .txt file

I have a .txt file which contains lines that follow a standard convention. I can read every line of the .txt file into an array, but I can't split the spaces in each line and put each word into a new array in order to list the first item of each line.
Access.txt
2014-03-16 13:57:35.089 -0700 Information 98
2014-03-16 13:57:35.089 -0700 Information 22
2014-03-16 13:57:35.355 -0700 Information 638
2014-03-16 13:57:35.355 -0700 Information 94

LogsModel:
public function fileGetContents()
{
    $filename = 'C:/Program Files/FileMaker/FileMaker Server/Logs/Access.log';

    // Open the file
    $fp = @fopen($filename, 'r');

    // Add each line to an array
    if ($fp) {
        $lines = explode("\n", fread($fp, filesize($filename)));

        // split each line by " "
        foreach($lines as $line){
            $line_items = explode(" ", $line);

            return $line_items;
        }
    }
}

LogsController:
public function index()
{
    // load a model, perform an action, pass the returned data to a variable
    $logs_model = $this->loadModel('LogsModel');
    $line_items = $logs_model->fileGetContents();

    // load views.
    require 'application/views/_templates/header.php';
    require 'application/views/home/index.php';
    require 'application/views/_templates/footer.php';
}

home/index.php
<table class="table table-striped">
    <tr>
        <td>Date</td>
    </tr>
    <?php foreach ($line_items as $item) { ?>
        <tr>
            <td><?php echo $item ?></td>
        </tr>
    <?php } ?>
</table>

Right now I'm getting ALL the items from the first line...
DATE
2014-03-16
13:57:35.089
-0700   
Information 
98

What I want to display is the first item from EACH line...
DATE
2014-03-16
2014-03-16
2014-03-16
2014-03-16

A:

Its pretty simple:
  $line_items = explode(" ", $lines[0]); //split first line
  echo $line_items[0]; //print first item

If you only want one item of one line, why is your method called fileGetContents()?  You're trying to return the whole file as one line, constantly in a loop.  Won't work. You need to redesign this method.

