{"id":15409484,"url":"https://github.com/xpaw/comparearrays.php","last_synced_at":"2025-04-19T09:51:01.192Z","repository":{"id":57084863,"uuid":"94702702","full_name":"xPaw/CompareArrays.php","owner":"xPaw","description":"🍌 Diffing multi dimensional arrays the easy way","archived":false,"fork":false,"pushed_at":"2025-03-20T10:24:07.000Z","size":28,"stargazers_count":9,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-06T23:30:01.009Z","etag":null,"topics":["php"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/xpaw/compare-arrays","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/xPaw.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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-06-18T17:34:05.000Z","updated_at":"2025-04-05T20:38:38.000Z","dependencies_parsed_at":"2023-01-22T21:15:14.410Z","dependency_job_id":"5662ff3e-909b-4e52-8c6c-ff06f9293511","html_url":"https://github.com/xPaw/CompareArrays.php","commit_stats":{"total_commits":26,"total_committers":2,"mean_commits":13.0,"dds":"0.038461538461538436","last_synced_commit":"5a79022ca8cff3f5b3d3fbe567819f8ac0090e2c"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xPaw%2FCompareArrays.php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xPaw%2FCompareArrays.php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xPaw%2FCompareArrays.php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xPaw%2FCompareArrays.php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xPaw","download_url":"https://codeload.github.com/xPaw/CompareArrays.php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249191414,"owners_count":21227561,"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":["php"],"created_at":"2024-10-01T16:40:14.233Z","updated_at":"2025-04-19T09:51:01.173Z","avatar_url":"https://github.com/xPaw.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CompareArrays\n\n[![Latest Stable Version](https://img.shields.io/packagist/v/xpaw/compare-arrays.svg)](https://packagist.org/packages/xpaw/compare-arrays)\n[![License](https://img.shields.io/github/license/xPaw/CompareArrays.php.svg)](https://github.com/xPaw/CompareArrays.php/blob/master/LICENSE)\n[![PHP Version](https://img.shields.io/packagist/php-v/xpaw/compare-arrays.svg)](https://packagist.org/packages/xpaw/compare-arrays)\n\nA PHP library for comparing multi-dimensional arrays and detecting differences.\n\n## Features\n\n- Deep comparison of multi-dimensional arrays\n- Detects added, removed, and modified values\n- Maintains array structure in the result\n- Special handling for float comparison with epsilon\n- Flatten results into a single-dimensional array with path keys\n\n## Installation\n\nInstall via Composer:\n\n```bash\ncomposer require xpaw/compare-arrays\n```\n\n## Usage\n\n### Basic Comparison\n\n```php\nuse xPaw\\CompareArrays\\CompareArrays;\n\n$oldArray = [\n\t'user' =\u003e [\n\t\t'name' =\u003e 'John',\n\t\t'age' =\u003e 30,\n\t\t'settings' =\u003e [\n\t\t\t'darkMode' =\u003e true,\n\t\t\t'notifications' =\u003e true\n\t\t]\n\t]\n];\n\n$newArray = [\n\t'user' =\u003e [\n\t\t'name' =\u003e 'John Doe',\n\t\t'age' =\u003e 30,\n\t\t'settings' =\u003e [\n\t\t\t'darkMode' =\u003e true,\n\t\t\t'notifications' =\u003e false\n\t\t],\n\t\t'lastLogin' =\u003e '2025-03-20'\n\t]\n];\n\n$differences = CompareArrays::Diff($oldArray, $newArray);\nprint_r($differences);\n```\n\nResult:\n\n```\nArray(\n    [user] =\u003e Array(\n\t\t[name] =\u003e xPaw\\CompareArrays\\ComparedValue Object(\n\t\t\t[OldValue] =\u003e John\n\t\t\t[NewValue] =\u003e John Doe\n\t\t\t[Type] =\u003e modified\n\t\t)\n\n\t\t[settings] =\u003e Array(\n\t\t\t[notifications] =\u003e xPaw\\CompareArrays\\ComparedValue Object(\n\t\t\t\t[OldValue] =\u003e 1\n\t\t\t\t[NewValue] =\u003e\n\t\t\t\t[Type] =\u003e modified\n\t\t\t)\n\t\t)\n\n\t\t[lastLogin] =\u003e xPaw\\CompareArrays\\ComparedValue Object(\n\t\t\t[OldValue] =\u003e\n\t\t\t[NewValue] =\u003e 2025-03-20\n\t\t\t[Type] =\u003e added\n\t\t)\n\t)\n)\n```\n\n### Flattening Results\n\nTo simplify handling of nested differences, you can flatten the result:\n\n```php\n$flattened = CompareArrays::Flatten($differences);\nprint_r($flattened);\n```\n\nResult:\n\n```\nArray(\n    [user/name] =\u003e xPaw\\CompareArrays\\ComparedValue Object(\n\t\t[OldValue] =\u003e John\n\t\t[NewValue] =\u003e John Doe\n\t\t[Type] =\u003e modified\n\t)\n\n    [user/settings/notifications] =\u003e xPaw\\CompareArrays\\ComparedValue Object(\n\t\t[OldValue] =\u003e 1\n\t\t[NewValue] =\u003e\n\t\t[Type] =\u003e modified\n\t)\n\n    [user/lastLogin] =\u003e xPaw\\CompareArrays\\ComparedValue Object(\n\t\t[OldValue] =\u003e\n\t\t[NewValue] =\u003e 2025-03-20\n\t\t[Type] =\u003e added\n\t)\n)\n```\n\n### Custom Separator and Path Prefix\n\nYou can customize the separator and add a path prefix when flattening:\n\n```php\n// Using a dot as separator\n$flattened = CompareArrays::Flatten($differences, '.');\n```\n\nResult:\n\n```\nArray\n(\n    [user.name] =\u003e xPaw\\CompareArrays\\ComparedValue Object(...)\n    [user.settings.notifications] =\u003e xPaw\\CompareArrays\\ComparedValue Object(...)\n    [user.lastLogin] =\u003e xPaw\\CompareArrays\\ComparedValue Object(...)\n)\n```\n\n## API Reference\n\n### CompareArrays::Diff(array $Old, array $New): array\n\nCompares two arrays and produces a new array of changes between these arrays.\n\n- The result maintains the same structure as the input arrays\n- The deepest values are `ComparedValue` objects\n- Float values are compared with `PHP_FLOAT_EPSILON` for precision\n- Special handling for arrays vs non-arrays in corresponding positions\n\n### CompareArrays::Flatten(array $Input, string $Separator = '/', ?string $Path = null): array\n\nFlattens a multi-dimensional array into a one-dimensional array.\n\n- Keys are transformed into paths separated by the specified separator\n- Optionally prepend a path prefix to all keys\n\n### ComparedValue\n\nEach detected difference is represented by a `ComparedValue` object with:\n\n- `$Type`: One of `added`, `removed`, or `modified`\n- `$OldValue`: The value from the old array (null for added items)\n- `$NewValue`: The value from the new array (null for removed items)\n\n## Special Cases\n\n### Float Comparison\n\nFloating-point values are compared with `PHP_FLOAT_EPSILON` to handle precision issues:\n\n```php\n$differences = CompareArrays::Diff(\n\t['value' =\u003e 0.1],\n\t['value' =\u003e 0.1 + 0.00000001]\n);\n// Result: empty array (no differences)\n\n$differences = CompareArrays::Diff(\n\t['value' =\u003e 0.1],\n\t['value' =\u003e 0.1 + 0.0001]\n);\n// Result: detects difference\n```\n\n## License\n\nThis library is licensed under the [MIT License](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxpaw%2Fcomparearrays.php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxpaw%2Fcomparearrays.php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxpaw%2Fcomparearrays.php/lists"}