Skip to content

Instantly share code, notes, and snippets.

@tamimibrahim17
Created July 17, 2018 11:16
Show Gist options
  • Save tamimibrahim17/1b343d2562a8ffeaba9e341f7d135396 to your computer and use it in GitHub Desktop.
Save tamimibrahim17/1b343d2562a8ffeaba9e341f7d135396 to your computer and use it in GitHub Desktop.
Chunk file
<?php
// this will convert file lines into a big array. So, if your file has 1000 lines the array count will be 1000
// if your file size 20 MB than this method will occupy 20MB of system ram .
$lines = file('big_file.txt');
// Now we are going to chunk the lines by 100000 on each chunk
// array_chunk will create a multi demential array with chunked line on array.
$chunked = array_chunk($lines,100000);
// use chunk
foreach( $chunked as $key => $value ){
if ( is_array($value) ){
// this must be a array with all chunked lines. count will be 100000 as you've defined it in array_chunk
// now you can loop this array and write to a file or do whatever.
foreach( $value as $nkey => $nval ){
echo $nval;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment