0

I have a file that has several lines of data. I would like every 33 lines of data to be grouped into an array, after that data is grouped into an array I'd like to colon delimitate the data into sub arrays like so:

<?
 print_r(explode(':', $line));
?>

How do I prep the every 33 lines of data in the file to get ready to become my $line variable in the snippet above?

0

2 Answers 2

2

I'm not sure this will fit your goal, but take a look at array_chunk. For your scenario, assuming your data is coming from a file (if not, you can explode to split lines into an array):

 $line_array = file($location_of_data_file);

 $line_array_by_33 = array_chunk($line_array, 33);

This wil give you an array like:

[0] =>
      [0] => "Line 1",
      [1] => "Line 2",
      [2] => "Line 3",
      [3] => "Line 4",
[1] =>
      [0] => "Line 5",
      [1] => "Line 6",
      [2] => "Line 7",
      [3] => "Line 8",

The above is a shorter version, but you get the idea.

If you need to add your delimiter, you can always implode each chunk, add your token, and then re-explode.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Sorry for the Delay!
0

Based on an IBM article, try something like:

$counter = 1;
$group_of_lines = '';
$file_handle = fopen("myfile", "r");

while (!feof($file_handle)) {
    $line = fgets($file_handle);
    $group_of_lines .= $line;

    if($counter % 33 == 0) {
        // do stuff with your $group_of_lines
    }

    $counter++;
}
fclose($file_handle);

1 Comment

Thank you this will help in the future!

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.