{"id":23588152,"url":"https://github.com/stilliard/csvparser","last_synced_at":"2025-10-13T16:08:17.650Z","repository":{"id":15018756,"uuid":"17744436","full_name":"stilliard/CsvParser","owner":"stilliard","description":"Quickly take in and output csv formats, also map functions to columns and rows and many more simple csv operations.","archived":false,"fork":false,"pushed_at":"2025-08-13T09:12:27.000Z","size":71,"stargazers_count":11,"open_issues_count":11,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-10-13T16:07:52.879Z","etag":null,"topics":["csv-format","php"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"nukr/node-multiconfig","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stilliard.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":"stilliard","patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2014-03-14T11:40:37.000Z","updated_at":"2025-08-13T09:12:27.000Z","dependencies_parsed_at":"2024-06-21T14:04:30.178Z","dependency_job_id":"73038d48-39da-4dd3-977a-7ff735cf840c","html_url":"https://github.com/stilliard/CsvParser","commit_stats":{"total_commits":74,"total_committers":4,"mean_commits":18.5,"dds":0.04054054054054057,"last_synced_commit":"2db1b0868843ee749273791109b49942eed4a40b"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"purl":"pkg:github/stilliard/CsvParser","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stilliard%2FCsvParser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stilliard%2FCsvParser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stilliard%2FCsvParser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stilliard%2FCsvParser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stilliard","download_url":"https://codeload.github.com/stilliard/CsvParser/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stilliard%2FCsvParser/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279016062,"owners_count":26085798,"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","status":"online","status_checked_at":"2025-10-13T02:00:06.723Z","response_time":61,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["csv-format","php"],"created_at":"2024-12-27T05:12:33.372Z","updated_at":"2025-10-13T16:08:17.645Z","avatar_url":"https://github.com/stilliard.png","language":"PHP","readme":"# Csv Parser\nQuickly take in and output csv formats.\n\n[![Build Status](https://travis-ci.org/stilliard/CsvParser.png?branch=master)](https://travis-ci.org/stilliard/CsvParser)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/stilliard/CsvParser/badges/quality-score.png?s=3f821d3231d78e86c41c9cd9213c68f164bb53d6)](https://scrutinizer-ci.com/g/stilliard/CsvParser/)\n[![Code Coverage](https://scrutinizer-ci.com/g/stilliard/CsvParser/badges/coverage.png?s=dbc9d91b767b84a1a649b5695b8a3cdce690684a)](https://scrutinizer-ci.com/g/stilliard/CsvParser/)\n[![Latest Stable Version](https://poser.pugx.org/stilliard/csvparser/v/stable.png)](https://packagist.org/packages/stilliard/csvparser) [![Total Downloads](https://poser.pugx.org/stilliard/csvparser/downloads.png)](https://packagist.org/packages/stilliard/csvparser) [![Latest Unstable Version](https://poser.pugx.org/stilliard/csvparser/v/unstable.png)](https://packagist.org/packages/stilliard/csvparser) [![License](https://poser.pugx.org/stilliard/csvparser/license.png)](https://packagist.org/packages/stilliard/csvparser)\n[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fstilliard%2FCsvParser.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fstilliard%2FCsvParser?ref=badge_shield)\n\n## Install\n```bash\ncomposer require stilliard/csvparser 1.3.2\n```\n\n## Example usage:\n```php\nuse CsvParser\\Parser;\n//\n// Simple array to string usage\n//\n$array = [['id'=\u003e1, 'name'=\u003e'Bob'],['id'=\u003e2, 'name'=\u003e'Bill']];\n$parser = new Parser();\n$csv = $parser-\u003efromArray($array);\nvar_dump($parser-\u003etoString($csv));\n```\n\n## Example stream reading (better memory optimisations)\n```php\n// stream reading from a CSV file\nforeach (Parser::stream(__DIR__ . '/your/path/input.csv') as $row) {\n    var_dump($row);\n}\n// write file\nParser::write($data, __DIR__ . '/your/path/output.csv');\n```\n\n\n```php\n//\n// Full power examples:\n//\n\n// setup initial parser\n$parser = new \\CsvParser\\Parser(',', '\"', \"\\n\");\n\n// change settings after init\n// set column delimiter\n$parser-\u003efieldDelimiter = ';';\n// set text enclosure\n$parser-\u003efieldEnclosure = \"'\";\n// set line delimiter\n$parser-\u003elineDelimiter = \"\\n\";\n\n// Input (returns instance of \\CsvParser\\Csv)\n$csv = $parser-\u003efromArray([['id'=\u003e1, 'name'=\u003e'Bob'],['id'=\u003e2, 'name'=\u003e'Bill']]);\n$csv = $parser-\u003efromString(\"id,name\\n1,Bob\\n2,Bill\");\n$csv = $parser-\u003efromFile('demo.csv');\n\n// get row count\nvar_dump($csv-\u003egetRowCount());\n\n// get the first row as array from the csv\nvar_dump($csv-\u003efirst());\n\n// get the column headings / keys\nvar_dump($csv-\u003egetKeys());\n\n// want to force a column sort / index?\n$csv-\u003ereKey(['id', 'name', 'email']);\n\n// append/prepend rows\n$csv-\u003eappendRow(['id'=\u003e3, 'name'=\u003e'Ben']);\n$csv-\u003eprependRow(['id'=\u003e4, 'name'=\u003e'Barry']);\n\n// map function over column\n$csv-\u003emapColumn('name', 'trim');\n$csv-\u003emapColumn('name', function ($name) {\n    return trim($name);\n});\n\n// map function over rows\n$csv-\u003emapRows(function ($row) {\n    $row['codename'] = base64_encode($row['id']);\n    return $row;\n});\n\n// add a column\n$csv-\u003eaddColumn('codename', 'default value');\n\n// remove a column\n$csv-\u003eremoveColumn('codename');\n\n// filter down rows\n$csv-\u003efilterRows(function ($row) {\n    return $row['id'] != '#'; // remove rows where the id column just has a hash inside\n});\n\n// remove row by index\n$csv-\u003eremoveRowByIndex(4);\n// or remove row(s) by column value, such as id 22\n$csv-\u003eremoveRow('id', 22);\n// or remove row(s) by multiple creiteria, such as when id 22 AND when name is 'some name'\n$csv-\u003eremoveRows(['id'=\u003e22, 'name'=\u003e'some name']);\n\n// Column reordering\n$csv-\u003ereorderColumn('colname', 0); // move to position 0 (the start)\n// or multiple\n$csv-\u003ereorderColumns(['colname1'=\u003e0, 'colname2'=\u003e4]);\n\n// Row reordering\n// to move the row with id of 22 to the start\n$csv-\u003ereorderRow('id', 22, 0);\n// or move id 22 to the start, and id 5 after it\n$csv-\u003ereorderRows('id', [22 =\u003e 0, 5 =\u003e 1]);\n\n// Sort rows by a column\n$csv-\u003ereorderRowsByColumn('id', 'desc');\n// or even multiples:\n$csv-\u003ereorderRowsByColumns(['name', 'id' =\u003e 'desc']);\n\n// Output\nvar_dump($parser-\u003etoArray($csv));\nvar_dump($parser-\u003etoString($csv));\nvar_dump($parser-\u003etoFile($csv, 'demo.csv')); // file was created?\n\n// Need to chunk into multiple chunks/files?\n$chunks = $parser-\u003etoChunks($csv, 1000);\nforeach ($chunks as $i =\u003e $chunk) {\n    $parser-\u003etoFile($chunk, \"output-{$i}.csv\");\n}\n\n// Remove duplicates\n$csv-\u003eremoveDuplicates('email');\n\n// Remove blanks\n$csv-\u003eremoveBlanks('email');\n\n```\n\n## Writing CSV as a Stream\n\nThe `writeStream` method allows you to write CSV data as a stream. This can be useful for writing large datasets efficiently.\n\n### Example: Writing to a File\n\n```php\nuse CsvParser\\Parser;\n\n$file = fopen('output.csv', 'w');\n\n$callback = function () {\n    static $data = [\n        ['name' =\u003e 'John', 'age' =\u003e 30],\n        ['name' =\u003e 'Jane', 'age' =\u003e 25],\n        ['name' =\u003e 'Doe', 'age' =\u003e 40],\n    ];\n    return array_shift($data);\n};\n\nParser::writeStream($file, ['name', 'age'], $callback);\n\nfclose($file);\n```\n\n### Example: Writing to the Screen\n\n```php\nuse CsvParser\\Parser;\n\n$resource = fopen('php://output', 'w');\n\n$callback = function () {\n    $data = [\n        ['name' =\u003e 'John', 'age' =\u003e 30],\n        ['name' =\u003e 'Jane', 'age' =\u003e 25],\n        ['name' =\u003e 'Doe', 'age' =\u003e 40],\n    ];\n    foreach ($data as $row) {\n        yield $row;\n    }\n};\n\nParser::writeStream($resource, ['name', 'age'], $callback);\n\nfclose($resource);\n```\n\n### Example: Writing from a PDO Fetch\n\n```php\nuse CsvParser\\Parser;\nuse PDO;\n\n$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'username', 'password');\n$stmt = $pdo-\u003equery('SELECT name, age FROM users');\n\n$file = fopen('output.csv', 'w');\n\nParser::writeStream($file, ['name', 'age'], fn() =\u003e $stmt-\u003efetch(PDO::FETCH_NUM));\n\nfclose($file);\n```\n\nIn this example, the `callback` function uses a PDO statement to fetch rows from a database. The `writeStream` method will continue to call the `callback` until it returns `false`.\n\n## Test\nTo run the tests, you can use PHPUnit. Make sure you have PHPUnit installed and then run:\n\n```bash\nphpunit .\n```\n\n\n## License\n[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fstilliard%2FCsvParser.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fstilliard%2FCsvParser?ref=badge_large)\n","funding_links":["https://github.com/sponsors/stilliard"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstilliard%2Fcsvparser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstilliard%2Fcsvparser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstilliard%2Fcsvparser/lists"}