Nic
Nic

Reputation: 163

PHP Parsing file into Array of Array

I have a file with many rows,each row have the following format: 1519382994.85#MSG#Something went wrong

So, for each row i have three field divided by #. A number, a message type and a string.

Now i want to read the file and split the contents. I made it in this way:

//Opening the logger file
$myfile = file_get_contents("operations.txt", "r") or die("Unable to open file!");
$rows = explode("\n", $myfile);
$num_rows = count($rows);
$fieldList = array();
//Parsing rows using '#'
foreach ($rows as $row => $data) {
    $row_data = explode('#', $data);
    array_push($fieldList, (string)$row_data[0]);
    array_push($fieldList, (string)$row_data[1]);
    array_push($fieldList, (string)$row_data[2]);
}

The code is working well but i'd like to have an array of array and this kind of data:

0: Array [ "112323.76", "MSG", "Hello"]
1: Array [ "453435.78", "MSG", "Bye"] etc..

I tryed with this code but i'm doing something wrong.

  $last=0;
 $result = array();
        for ($i = 0; $i < $num_rows; $i++) {
        array_push($result, (string) $fieldList[$last], (string) $fieldList[$last+1],(string) $fieldList[$last+2]);
        //echo $fieldList[$last].'<br>';
        //echo $fieldList[$last+1].'<br>';
        //echo $fieldList[$last+2].'<br>';
        $last=$last+3;
        }

I'm a newbie in PHP someone can help me please and tell me what i'm doing wrong? Tanx a Lot for your time

Upvotes: 3

Views: 270

Answers (2)

Kris
Kris

Reputation: 41827

You could probably make use of the built-in fgetcsv:

array fgetcsv ( resource $handle [, int $length = 0 [, string $delimiter = "," [, string $enclosure = '"' [, string $escape = "\\" ]]]] )

This could look like:

$rows = [];
if (false !== ($handle = fopen("path/to/file", "r"))) 
{
    while (false !== ($row = fgetcsv($handle, 1000, ",")))
    {
        array_push($rows, $row);
    }
    fclose($handle);
}

Don't know if it would be a lot faster, but looks a lot easier to me. The main benefits of this over file() and explode() are:

  1. There is no need to have the entire file in RAM at once, processing could be done one row at a time.
  2. it is easy to support other "Character Seperated Values" type files where fields may be quoted ($enclosure)

Upvotes: 3

Jigar Shah
Jigar Shah

Reputation: 6223

Just needed some modifications in your code. Added comments to modified lines-

$myfile = file_get_contents("operations.txt", "r") or die("Unable to open file!");
$rows = explode("\n", $myfile);
$num_rows = count($rows);
$finalFieldList = array(); // new array
//Parsing rows using '#'
foreach ($rows as $row => $data) {
    $fieldList = array(); // temporary array
    $row_data = explode('#', $data);
    array_push($fieldList, (string)$row_data[0]);
    array_push($fieldList, (string)$row_data[1]);
    array_push($fieldList, (string)$row_data[2]);

    array_push($finalFieldList, $fieldList); // it will push to final array containing all 3 values
}

Upvotes: 1

Related Questions