3

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

0

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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
}

5 Comments

This is working in some waybut i get this error. 1st iter is correct. {"0":{"0":"1519382994.96","1":"MSG","2":"blablawentok"} 2nd is wrong "1":{"0":"1519382994.96","1":"MSG","2":"blablawenok","3":"1519382994.96","4":"MSG","5":"Processato CSV"}. So i have anincrementalerror at each iter. Some hints? :)
you have made the same mistake in that code as well. You can use another temporary array to get the correct formatting , or it would be better to do in your first foreach loop
Resolvedin this way, but i think is not elegant... 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]); array_push($result, $fieldList); // it will push to final array containing all 3 values unset($foo); // break references $fieldList = array(); // re-initialize to empty array }
yes it is not, you can have look on above answers for decent ways to get your desired output. :)
I forgot to add the temporary array. It works like a charm now. Ty Alive

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.