{"id":16184300,"url":"https://github.com/mattmezza/php-line-map","last_synced_at":"2025-07-15T06:41:14.656Z","repository":{"id":62525307,"uuid":"114646101","full_name":"mattmezza/php-line-map","owner":"mattmezza","description":"Map\\CSV::file($usersCsv)-\u003ewith(function ($row) { echo $row['name']; }); – Do stuff for each line of text","archived":false,"fork":false,"pushed_at":"2018-03-21T11:10:22.000Z","size":21,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-07T12:50:14.414Z","etag":null,"topics":["closure","collection","csv","foreach","map","php","textfile"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mattmezza.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"license.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-12-18T13:46:10.000Z","updated_at":"2024-07-09T09:50:27.000Z","dependencies_parsed_at":"2022-11-02T15:31:43.590Z","dependency_job_id":null,"html_url":"https://github.com/mattmezza/php-line-map","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/mattmezza/php-line-map","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattmezza%2Fphp-line-map","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattmezza%2Fphp-line-map/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattmezza%2Fphp-line-map/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattmezza%2Fphp-line-map/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mattmezza","download_url":"https://codeload.github.com/mattmezza/php-line-map/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattmezza%2Fphp-line-map/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265414706,"owners_count":23761056,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["closure","collection","csv","foreach","map","php","textfile"],"created_at":"2024-10-10T07:09:45.621Z","updated_at":"2025-07-15T06:41:14.631Z","avatar_url":"https://github.com/mattmezza.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"PHP Line Map\n======\n\n[![Packagist](https://img.shields.io/packagist/v/mattmezza/line-map.svg)](https://github.com/mattmezza/php-line-map) [![PHP from Packagist](https://img.shields.io/packagist/php-v/mattmezza/line-map.svg)](https://github.com/mattmezza/php-line-map) [![GitHub license](https://img.shields.io/github/license/mattmezza/php-line-map.svg)](https://github.com/mattmezza/php-line-map/blob/master/license.md)\n\nMaps a function through the lines of a file, line by line\n\nInstall it with `composer require mattmezza/line-map`\n\nUse it like this:\n\n```php\n$map = Map\\CSV::fileNamed(\"example.csv\");\n$rows = $map-\u003ewith(function ($row, $idx) {\n    return array_merge([$idx + 1], $row);\n})-\u003eget();\n$header = $map-\u003egetHeader();\n```\n\nor\n\n```php\nMap\\Txt::fileNamed(\"lines.txt\")-\u003ewith(function ($line, $idx) {\n    echo \"$idx: $line\";\n});\n```\n\n# TXT\n```php\n// the callback function - can be also an anonymous closure\n$doStuff = function($line, $lineNumber) {\n    return $lineNumber . \": \" . $line;\n};\n\n// from a string variable\n$text = \"line 1\nline2\nline3\";\n$newLines = Map\\Txt::string($text)-\u003ewith($doStuff)-\u003etoArray();\n\n// from a filename\n$filename = \"./somelines.txt\";\n$newLines = Map\\Txt::fileNamed($filename)-\u003ewith($doStuff)-\u003etoArray();\n\n// from a file resource handle\n$file = fopen($filename, \"r\");\n$newLines = Map\\Txt::file($file)-\u003ewith($doStuff)-\u003etoArray();\n```\n# CSV\n```php\n// the callback function - can be also an anonymous closure\n$doStuff = function($row, $lineNumber, $headers) {\n    echo $row[$headers[0]];\n    return $row;\n};\n\n// from a string variable\n$csv = \"name,surname\nJohn,Doe\nFoo,Bar\";\n$newRows = Map\\CSV::string($csv)-\u003ewith($doStuff)-\u003etoArray();\n\n// from a filename\n$filename = \"./somedata.csv\";\n$newRows = Map\\CSV::fileNamed($filename)-\u003ewith($doStuff)-\u003etoArray();\n\n// from a file resource handle\n$file = fopen($filename, \"r\");\n$newRows = Map\\CSV::file($file)-\u003ewith($doStuff)-\u003etoArray();\n```\n\n##### example\n\nRapidly cycle through this CSV \n\n```csv\nname,username,email\nMatt,mattmezza,mattmezza@gmail.com\n```\n\nfor each line generate custom message replacing values using the following template\n\n```\nHello {{name}},\nyour username is {{username}}.\n```\n\nsend each message to the right user and collect some logs\n\n```php\n$tpl = \"Hello __name__,\nyour username is __username__.\";\n$logs = Map\\CSV::fileNamed(\"file.csv\")-\u003ewith(function($row, $idx, $headers) use ($tpl) {\n    $msg = $tpl;\n    foreach ($headers as $header) {\n        $msg = str_replace(\"{{\".$header.\"}}\", $row[$header], $msg);\n    }\n    return sendMail($row[\"email\"]);\n})-\u003etoArray();\n```\n\n###### Matteo Merola \u003cmattmezza@gmail.com\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattmezza%2Fphp-line-map","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmattmezza%2Fphp-line-map","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattmezza%2Fphp-line-map/lists"}