{"id":17031666,"url":"https://github.com/alaasarhan/php-flatten","last_synced_at":"2025-04-12T12:32:06.602Z","repository":{"id":53594069,"uuid":"69195997","full_name":"AlaaSarhan/php-flatten","owner":"AlaaSarhan","description":null,"archived":false,"fork":false,"pushed_at":"2023-12-03T20:02:13.000Z","size":61,"stargazers_count":21,"open_issues_count":0,"forks_count":5,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-26T07:22:02.304Z","etag":null,"topics":["arrays","flatten","flatten-array","php-library"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AlaaSarhan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2016-09-25T23:10:55.000Z","updated_at":"2024-11-13T18:54:32.000Z","dependencies_parsed_at":"2023-12-02T14:27:38.282Z","dependency_job_id":"fa5ba253-6838-40b7-834f-88eafb0ca300","html_url":"https://github.com/AlaaSarhan/php-flatten","commit_stats":{"total_commits":44,"total_committers":2,"mean_commits":22.0,"dds":"0.20454545454545459","last_synced_commit":"2b165a67883e6d357aae243e42817560e2bd5ea6"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlaaSarhan%2Fphp-flatten","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlaaSarhan%2Fphp-flatten/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlaaSarhan%2Fphp-flatten/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlaaSarhan%2Fphp-flatten/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AlaaSarhan","download_url":"https://codeload.github.com/AlaaSarhan/php-flatten/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248566579,"owners_count":21125690,"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":["arrays","flatten","flatten-array","php-library"],"created_at":"2024-10-14T08:25:02.869Z","updated_at":"2025-04-12T12:32:06.073Z","avatar_url":"https://github.com/AlaaSarhan.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# php-flatten\n\n[![Latest Version](https://img.shields.io/github/release/AlaaSarhan/php-flatten.svg?style=flat-square)](https://github.com/AlaaSarhan/php-flatten/releases)\n[![Software License](https://img.shields.io/badge/license-LGPL_v3.0-brightgreen.svg?style=flat-square)](LICENSE)\n[![Total Downloads](https://img.shields.io/packagist/dt/sarhan/php-flatten.svg?style=flat-square)](https://packagist.org/packages/sarhan/php-flatten)\n\nA utility function to mainly flatten multidimensional-arrays and traversables into a one-dimensional array, preserving keys\nand joining them with a customizable separator to from fully-qualified keys in the final array.\n\n## Installation\n\n```bash\n  composer require sarhan/php-flatten\n```\n\n## Usage\n\n**Example 1**\n\n```php\nuse Sarhan\\Flatten\\Flatten;\n\n$multiArray = [\n    'say' =\u003e 'what',\n    'hi' =\u003e [ 'de' =\u003e 'Hallo', 'es' =\u003e 'Hola' ]\n];\n\n/*\nFlatten::__construct(\n    string $separator = '.',\n    string $prefix = '',\n    int $flags = 0\n)\n*/\n$flatten = new Flatten();\n\n// Flatten::flattenToArray is provided for convinience. It internally\n// calls Flatten::flatten and converts it's output, which is a 1-dimensional\n// iterator, into a 1-dimensional array.\n$flattened = $flatten-\u003eflattenToArray($multiArray);\n\n// Flatten::unflattenToArray is provided for convinience. It internally\n// calls Flatten::unflatten and converts it's output, which is a recursive\n// generator structure, into a multi-dimensional array.\n$unflattened = $flatten-\u003eunflattenToArray($flattened);\n\nassert($flattened == [\n    'say' =\u003e what\n    'hi.de' =\u003e Hallo\n    'hi.es' =\u003e Hola\n]);\n\nassert($unflattened == $multiArray);\n```\n\n**Example 2**\n\nCustom Separator and initial prefix\n```php\nuse Sarhan\\Flatten\\Flatten;\n\n$allowAccess = [\n    'root' =\u003e false,\n    'var' =\u003e [ 'log' =\u003e ['nginx' =\u003e true, 'apt' =\u003e false], 'www' =\u003e true ],\n];\n\n$flatten = new Flatten(\n  '/',  // separator\n  '/'   // prefix\n);\n\n$flattened = $flatten-\u003eflattenToArray($allowAccess);\n\n$unflattened = $flatten-\u003eunflattenToArray($flattened);\n\nassert($flatten == [\n    '/root' =\u003e false,\n    '/var/log/nginx' =\u003e true,\n    '/var/log/apt' =\u003e false,\n    '/var/www' =\u003e true\n]);\n\nassert($unflattened == $allowAccess);\n```\n\n**Example 3**\n\nNotice that the prefix will not be separated in FQkeys. If it should be separated, separator must be appeneded to the prefix string.\n\n```php\nuse Sarhan\\Flatten\\Flatten;\n\n$api = [\n    'category' =\u003e [ 'health' =\u003e 321, 'sport' =\u003e 769, 'fashion' =\u003e 888 ],\n    'tag' =\u003e [ 'soccer' =\u003e 7124, 'tennis' =\u003e [ 'singles' =\u003e 9833, 'doubles' =\u003e 27127 ] ],\n];\n\n$flatten = new Flatten('/', 'https://api.dummyhost.domain/');\n\n$flattened = $flatten-\u003eflattenToArray($api);\n\n$unflattened = $flatten-\u003eunflattenToArray($flattened);\n\nassert($flattened == [\n    'https://api.dummyhost.domain/category/health' =\u003e 321,\n    'https://api.dummyhost.domain/category/sport' =\u003e 769,\n    'https://api.dummyhost.domain/category/fashion' =\u003e 888,\n    'https://api.dummyhost.domain/tag/soccer' =\u003e 7124,\n    'https://api.dummyhost.domain/tag/tennis/singles' =\u003e 9833,\n    'https://api.dummyhost.domain/tag/tennis/doubles' =\u003e 27127\n]);\n\nassert($unflattened == $api);\n```\n\n**Example 4**\n\nNumeric keys are treated as associative keys.\n\n**Note:** This behavior can be changed using flags. See [FLAG_NUMERIC_NOT_FLATTENED](#numeric_not_flattened)\n\n```php\nuse Sarhan\\Flatten\\Flatten;\n\n$nutrition = [\n    'nutrition',\n    'fruits' =\u003e [ 'oranges', 'apple', 'banana' ],\n    'veggies' =\u003e ['lettuce', 'broccoli'],\n];\n\n$flatten = new Flatten('-');\n\n$flattened = $flatten-\u003eflattenToArray($nutrition);\n\n$unflattened = $flatten-\u003eunflattenToArray($flattened);\n\nassert($flattened == [\n    '0' =\u003e 'nutrition',\n    'fruits-0' =\u003e 'oranges',\n    'fruits-1' =\u003e 'apple',\n    'fruits-2' =\u003e 'banana',\n    'veggies-0' =\u003e 'lettuce',\n    'veggies-1' =\u003e 'broccoli'\n]);\n\nassert($unflattened == $nutrition);\n```\n\n### Flags\n\n\u003ca name=\"numeric_not_flattened\"\u003e\u003c/a\u003e**FLAG_NUMERIC_NOT_FLATTENED**\n\nTurns off flattening values with numeric (integer) keys.\n\nThose values will be wrapped in an array (preserving their keys) and associated to the parent FQK.\n\n```php\nuse Sarhan\\Flatten\\Flatten;\n\n$examples = [\n    'templates' =\u003e [\n      ['lang' =\u003e 'js', 'template' =\u003e \"console.log('%s');\"],\n      ['lang' =\u003e 'php', 'template' =\u003e 'echo \"%s\";']\n    ],\n    'values' =\u003e [3 =\u003e 'hello world', 5 =\u003e 'what is your name?']\n];\n\n$flatten = new Flatten(\n  '.',\n  'examples.',\n  Flatten::FLAG_NUMERIC_NOT_FLATTENED\n);\n\n$flattened = $flatten-\u003eflattenToArray($examples);\n\n$unflattened = $flatten-\u003eunflattenToArray($flattened);\n\nassert($flattened == [\n    'examples.templates' =\u003e [\n        [\n            'lang' =\u003e 'js',\n            'template' =\u003e 'console.log(\\'%s\\')';\n        ],\n        [\n            'lang' =\u003e 'php',\n            'template' =\u003e 'echo \"%s\"'\n        ]\n    ],\n    'examples.values' =\u003e [\n        3 =\u003e 'hello world',\n        5 =\u003e 'what is your name?'\n    ]\n]);\n\nassert($unflattened == $examples);\n```\nTop level numeric (integer) keys will also be returned into an array assigned to the passed prefix.\n\n```php\nuse Sarhan\\Flatten\\Flatten;\n\n$seats = [\n  'A1',\n  'A2',\n  'B1',\n  'B2',\n  '_reserved' =\u003e ['A1', 'B1'],\n  '_blocked' =\u003e ['B2']\n];\n\n$flatten = new Flatten(\n  '_',\n  'seats',\n  Flatten::FLAG_NUMERIC_NOT_FLATTENED\n);\n\n$flattened = $flatten-\u003eflattenToArray($seats);\n\n$unflattened = $flatten-\u003eunflattenToArray($flattened);\n\nassert($flattened == [\n    'seats' =\u003e ['A1', 'A2', 'B1', 'B2'],\n    'seats_reserved' =\u003e ['A1', 'B1'],\n    'seats_blocked' =\u003e ['B2']\n]);\n\nassert($unflattened == $seats);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falaasarhan%2Fphp-flatten","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falaasarhan%2Fphp-flatten","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falaasarhan%2Fphp-flatten/lists"}