php - Using array_filter() with a callback that returns an array -
i have data this:
a|b|c|d|e|f|g h|i|j|k|l|m|n o|p|q|r|s|t|u i can use explode("\n", $data) array of lines (this ~4.0gb file), want make multi-dimensional array using explode() based on vertical pipe. here tried:
$data = explode("\n", './path-to-file.txt'); $results = array_filter($data, function ($el) { return explode('|', $el); }); however, results in single-dimensional array original line in string form.
how can use array_filter() callback happens return array?
edit: could use foreach($data $datum) , explode() way, when tried that, utilized 4 times amount of ram size of file, , not desirable. seems perfect opportunity use callback function, seemingly cannot array_filter().
array_filter() filters elements include or exclude them , expects true include element or false exclude. use array_map():
$data = explode("\n", './path-to-file.txt'); $results = array_map(function ($el) { return explode('|', $el); }, $data); a more memory efficient way read line line:
if(($handle = fopen("./path-to-file.txt", "r")) !== false) { while(($results[] = fgetcsv($handle, 0, "|")) !== false) {} }
Comments
Post a Comment