{"id":21695603,"url":"https://github.com/friendly-pixel/ar","last_synced_at":"2025-04-12T12:06:08.005Z","repository":{"id":62507415,"uuid":"257708689","full_name":"Friendly-Pixel/Ar","owner":"Friendly-Pixel","description":"Ar makes working with PHP arrays easy","archived":false,"fork":false,"pushed_at":"2024-12-12T15:59:18.000Z","size":116,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-11T16:19:09.916Z","etag":null,"topics":["array","php","php-arrays"],"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/Friendly-Pixel.png","metadata":{"files":{"readme":"README.md","changelog":"Changelog.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-21T20:31:13.000Z","updated_at":"2025-01-17T14:30:58.000Z","dependencies_parsed_at":"2023-01-21T07:16:15.852Z","dependency_job_id":null,"html_url":"https://github.com/Friendly-Pixel/Ar","commit_stats":null,"previous_names":[],"tags_count":31,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Friendly-Pixel%2FAr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Friendly-Pixel%2FAr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Friendly-Pixel%2FAr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Friendly-Pixel%2FAr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Friendly-Pixel","download_url":"https://codeload.github.com/Friendly-Pixel/Ar/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248565060,"owners_count":21125415,"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":["array","php","php-arrays"],"created_at":"2024-11-25T19:15:01.618Z","updated_at":"2025-04-12T12:06:07.977Z","avatar_url":"https://github.com/Friendly-Pixel.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003c!-- \n!!!!!!!!\n!!!!!!!!\n!!!!!!!!\n!!!!!!!!\n!!!!!!!!\n!!!!!!!!\n!!!!!!!!\n!!!!!!!! Never modify this file, `README.md`\n!!!!!!!!\n!!!!!!!! Modify `README_template.md` instead then run `copy_docs.php`\n!!!!!!!!\n!!!!!!!!\n!!!!!!!!\n!!!!!!!!\n!!!!!!!!\n!!!!!!!!\n!!!!!!!!\n--\u003e\n# Ar makes working with PHP arrays easy\n\n* __Consistent__: All functions accept the array as first parameter.\n* __Immutable__: the input array is never modified. Fluent style returns a new object for every call.\n* __Tested__: unit-tested with 100% code coverage. ![](https://github.com/Friendly-Pixel/Ar/workflows/PHPUnit%20tests/badge.svg)\n* __Familiar__: function names follow PHP whereever possible.\n\nFluent style:\n\n```php\nuse FriendlyPixel\\Ar\\Ar;\n\n$ints = Ar::wrap([1, 6, 8])\n    -\u003emap(fn ($num) =\u003e $num * $num)\n    -\u003efilter(fn ($value, $key) =\u003e $value % 2 == 0);\n```\n\nFunctional style:\n\n```php\nuse FriendlyPixel\\Ar\\Ar;\n\n$ints = [1, 5, 8];\n$ints = Ar::map($ints, fn($num) =\u003e $num * $num);\n$ints = Ar::filter($ints, fn($value, $key) =\u003e $value % 2 == 0)\n```\n\n## Install\n\nInstall the latest version using [Composer](https://getcomposer.org/):\n\n```\n$ composer require friendly-pixel/ar\n```\n\n## Methods\n\n- [count()](#count)\n- [filter()](#filter)\n- [first()](#first)\n- [flat()](#flat)\n- [forEach()](#forEach)\n- [implode()](#implode)\n- [keys()](#keys)\n- [last()](#last)\n- [map()](#map)\n- [mapKeys()](#mapKeys)\n- [merge()](#merge)\n- [push()](#push)\n- [reduce()](#reduce)\n- [search()](#search)\n- [slice()](#slice)\n- [sort()](#sort)\n- [splice()](#splice)\n- [unique()](#unique)\n- [unshift()](#unshift)\n- [values()](#values)\n\nFluent style only:\n\n- [wrap()](#wrap)\n- [unwrap()](#unwrap)\n\n\n\u003ca name=\"count\"\u003e\u003c/a\u003e\n### count\n\nCount how many items there are in the array.\n\n```php\nuse FriendlyPixel\\Ar\\Ar;\n\n$count = Ar::count([1, 2, 3]); \n$count = Ar::wrap([1, 2, 3])\n    -\u003ecount()\n;\n// Result: 3\n```\n\n\n\n\u003ca name=\"filter\"\u003e\u003c/a\u003e\n### filter\n\nPass every value, key into a user-supplied callable, and only put the item into the result array if the returned value is `true`.\nKeys are preserved only when `array_is_list($array)` returns false;\n\n```php\nuse FriendlyPixel\\Ar\\Ar;\n\n$even = Ar::filter([1, 2, 3, 12], function($value, $key) { return $value % 2 == 0; }); \n$even = Ar::wrap([1, 2, 3, 12])\n    -\u003efilter(function($value, $key) { return $value % 2 == 0; })\n    -\u003eunwrap();\n// Result: [1 =\u003e 2, 3 =\u003e 12]\n```\n\n\n@template A\n\n@param A[] $array \n\n@param callable(A $value, mixed $key): bool $callable\n\n@return A[]\n\n\n\n\u003ca name=\"first\"\u003e\u003c/a\u003e\n### first\n\nReturns the first value of the array or `false` when it's empty.\n\n```php\nuse FriendlyPixel\\Ar\\Ar;\n\nAr::first([2, 3, 4]);\nAr::wrap([2, 3, 4])-\u003efirst();\n\n// Result: 2\n```\n\n\n@template A\n\n@param A[] $array \n\n@return A\n\n\n\n\u003ca name=\"flat\"\u003e\u003c/a\u003e\n### flat\n\nThe flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.\n\n@param int $depth To what level to flatten the array. Default: 1\n\n@return mixed[]\n\n\n\n\u003ca name=\"forEach\"\u003e\u003c/a\u003e\n### forEach\n\nWalk over every value, key.\nPass every value, key into a user-supplied callable.\n\n\n@template A\n\n@param A[] $array \n\n@param callable(A $value, mixed $key): void $callable\n\n@return A[] Original array, unmodified\n\n\n\n\u003ca name=\"implode\"\u003e\u003c/a\u003e\n### implode\n\nJoin all values into a big string, using `$glue` as separator.\n`$glue` is optional.\n\n```php\nuse FriendlyPixel\\Ar\\Ar;\n\n$result = Ar::implode(['a', 'b', 'c'], ','); \n$result = Ar::wrap(['a', 'b', 'c'])\n    -\u003eimplode(',')\n;\n// result: \"a,b,c\"\n```\n\n\n\n\u003ca name=\"keys\"\u003e\u003c/a\u003e\n### keys\n\nReturn the keys of an array as a sequential array.\n\n```php\nuse FriendlyPixel\\Ar\\Ar;\n\n$result = Ar::keys([3 =\u003e 'a', 'foo' =\u003e 'b', 1 =\u003e 'c']); \n$result = Ar::wrap([3 =\u003e 'a', 'foo' =\u003e 'b', 1 =\u003e 'c'])-\u003ekeys()-\u003eunwrap();\n// result: [3, 'foo', 1]\n```\n\n\n@return mixed[]\n\n\n\n\u003ca name=\"last\"\u003e\u003c/a\u003e\n### last\n\nReturns the last value of the array or `false` when it's empty.\n\n```php\nuse FriendlyPixel\\Ar\\Ar;\n\nAr::last([2, 3, 4]);\nAr::wrap([2, 3, 4])-\u003elast();\n\n// Result: 4\n```\n\n\n@template A\n\n@param A[] $array \n\n@return A\n\n\n\n\u003ca name=\"map\"\u003e\u003c/a\u003e\n### map\n\nTransform values.\nPass every value, key into a user-supplied callable, and put the returned value into the result array.\nKeys are preserved.\n\n```php\nuse FriendlyPixel\\Ar\\Ar;\n\n$numbers = Ar::map([1, 2, 3], function($value, $key) { return $value * 2; }); \n$numbers = Ar::wrap([1, 2, 3])\n    -\u003emap(function($value, $key) { return $value * 2; })\n    -\u003eunwrap();\n// Result: [2, 4, 6]\n```\n\n\n@template A\n\n@template B\n\n@param A[] $array \n\n@param callable(A $value, mixed $key): B $callable\n\n@return B[]\n\n\n\n\u003ca name=\"mapKeys\"\u003e\u003c/a\u003e\n### mapKeys\n\nTransform keys.\nPass every value, key and key into a user-supplied callable, and use the returned value as key in the result array.\n\n```php\nuse FriendlyPixel\\Ar\\Ar;\n\n$numbers = Ar::mapKeys([1, 2, 3], function($value, $key) { return $key * 2; }); \n$numbers = Ar::wrap([1, 2, 3])\n    -\u003emapKeys(function($value, $key) { return $key * 2; })\n    -\u003eunwrap();\n// Result: [0 =\u003e 2, 2 =\u003e 2, 4 =\u003e 3]\n```\n\n\n@template A\n\n@template K\n\n@param A[] $array \n\n@param callable(A $value, mixed $key): K $callable\n\n@return array\u003cK, A\u003e\n\n\n\n\u003ca name=\"merge\"\u003e\u003c/a\u003e\n### merge\n\nMerges the elements of one or more arrays together so that the values of one are appended to the end of the previous one.\nIf the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.\nValues in the input arrays with numeric keys will be renumbered with incrementing keys starting from zero in the result array.\n\n```php\nuse FriendlyPixel\\Ar\\Ar;\n\n$numbers = Ar::merge(['a', 'b'], ['c', 'd'])); \n$numbers = Ar::wrap(['a', 'b'])\n    -\u003emerge(['b', 'c'])\n    -\u003eunwrap();\n// Result:['a', 'b', 'c', 'd']\n```\n\n\n@template A\n\n@var A[][] $arrays\n\n@return A[]\n\n\n\n\u003ca name=\"push\"\u003e\u003c/a\u003e\n### push\n\nAppend one or more items to the end of array.\n\n```php\nuse FriendlyPixel\\Ar\\Ar;\n\n$result = Ar::push([1, 2], 3, 4); \n$result = Ar::wrap([1, 2])-\u003epush(3, 4)-\u003eunwrap();\n\n// result: [1, 2, 3, 4]\n```\n\n\n@template A\n\n@param A[] $array \n\n@param A[] $values\n\n@return A[]\n\n\n\n\u003ca name=\"reduce\"\u003e\u003c/a\u003e\n### reduce\n\nIteratively reduce the array to a single value using a callback function.\n\n\n@template A\n\n@template B\n\n@param A[] $array \n\n@param callable(B|null $carry, A $value, mixed $key): B $callable\n\n@param B|null $initial If the optional initial is available, it will be used at the beginning of the process, or as a final result in case the array is empty.\n\n@return B\n\n\n\n\u003ca name=\"search\"\u003e\u003c/a\u003e\n### search\n\nReturn the first value for which the callable returns `true`.\nReturns `null` otherwise.\n\n```php\nuse FriendlyPixel\\Ar\\Ar;\n\n$found = Ar::search([ ['a' =\u003e 1], ['a' =\u003e 8], ['a' =\u003e 3] ], function($value, $key) { return $value['a'] == 3; }); \n$found = Ar::wrap([ ['a' =\u003e 1], [], ['a' =\u003e 3] ])\n    -\u003esearch(function($value, $key) { return $value['a'] == 3; })\n;\n// Result: ['a' =\u003e 3]\n```\n\n\n@template A\n\n@param A[] $array \n\n@param callable(A $value, mixed $key): bool $callable\n\n@return A|null\n\n\n\n\u003ca name=\"slice\"\u003e\u003c/a\u003e\n### slice\n\nExtract a slice of the array, include `$length` items, and starting from `$offset`.\n\n```php\nuse FriendlyPixel\\Ar\\Ar;\n\n$even = Ar::slice(['a', 'b', 'c', 'd'], 1, 2); \n$even = Ar::wrap(['a', 'b', 'c', 'd'])\n    -\u003eslice(1, 2)\n    -\u003eunwrap();\n// Result: ['b', 'c']\n```\n\n\n@template A\n\n@param A[] $array \n\n@param int $offset\n     If offset is non-negative, the sequence will start at that offset in the array.\n     If offset is negative, the sequence will start that far from the end of the array. \n\n@param ?int $length \n     If length is given and is positive, then the sequence will have up to that many elements\n     in it.\n     If the array is shorter than the length, then only the available array elements will be\n     present.\n     If length is given and is negative then the sequence will stop that many elements from \n     the end of the array.\n     If it is omitted, then the sequence will have everything from offset up until the end of\n     the array.\n\n@return A[]\n\n\n\n\u003ca name=\"sort\"\u003e\u003c/a\u003e\n### sort\n\nSort an array by values using a user-defined comparison function.\n\nThis function assigns new keys to the elements in array. It will remove any existing keys that may have been assigned.\n\n\n\n@template A\n\n@param A[] $array \n\n@param callable(A $valueA, A $valueB): int $callable    \n                             Return an integer smaller then, equal to,\n                             or larger than 0 to indicate that $valueA is less\n                             then, equal to, or larger than $valueB.\n\n@return A[]\n\n\n\n\u003ca name=\"splice\"\u003e\u003c/a\u003e\n### splice\n\nRemove a portion of the array and replace it with something else.\nOther than the default php function, this returns the changed array, not the extracted\nelements.\n\n```php\nuse FriendlyPixel\\Ar\\Ar;\n\n$even = Ar::splice(['a', 'b', 'c', 'd'], 1, 1, ['q', 'x']); \n$even = Ar::wrap(['a', 'b', 'c', 'd'])\n    -\u003esplice(1, 1, ['q', 'x'])\n    -\u003eunwrap();\n// Result: ['a', 'q', 'x', 'c', 'd']\n```\n\n\n@template A\n\n@param A[] $array \n\n@param int $offset\n     If offset is positive then the start of the removed portion is at that offset from \n     the beginning of the array. \n\n     If offset is negative then the start of the removed portion is at that offset from \n     the end of the array. \n\n@param ?int $length \n     If length is omitted, removes everything from offset to the end of the array.\n     *\n     If length is specified and is positive, then that many elements will be removed.\n      \n     If length is specified and is negative, then the end of the removed portion will be \n     that many elements from the end of the array.\n      \n     If length is specified and is zero, no elements will be removed. \n\n@param A[] $replacement\n     If replacement array is specified, then the removed elements are replaced with elements \n     from this array.\n\n     If offset and length are such that nothing is removed, then the elements from the \n     replacement array are inserted in the place specified by the offset. \n     *\n     If replacement is just one element it is not necessary to put array() or square brackets \n     around it, unless the element is an array itself, an object or null. \n     \n     Note: Keys in the replacement array are not preserved.\n\n@return A[] Other than the default php function, this returns the changed array, not the \n    extracted elements.\n\n\n\n\u003ca name=\"unique\"\u003e\u003c/a\u003e\n### unique\n\nRemove duplicate values from array.\nKeys are preserved only when `array_is_list($array)` returns false;\n\n```php\nuse FriendlyPixel\\Ar\\Ar;\n\n$result = Ar::unique(['a', 'a', 'b']); \n$result = Ar::wrap(['b', 4])-\u003eunique(['a', 'a', 'b'])-\u003eunwrap();\n\n// result: [0 =\u003e 'a', 2 =\u003e 'b']\n```\n\n\n@template A\n\n@param A[] $array \n\n@param int $flags The optional second parameter flags may be used to modify the sorting behavior using these values:\n    Sorting type flags:\n    \n    SORT_REGULAR - compare items normally (don't change types)\n    SORT_NUMERIC - compare items numerically\n    SORT_STRING - compare items as strings\n    SORT_LOCALE_STRING - compare items as strings, based on the current locale.\n\n@return A[]\n\n\n\n\u003ca name=\"unshift\"\u003e\u003c/a\u003e\n### unshift\n\nPrepend one or more items to the beginning of array.\n\n```php\nuse FriendlyPixel\\Ar\\Ar;\n\n$result = Ar::unshift([3, 4], 1, 2); \n$result = Ar::wrap([3, 4])-\u003eunshift(1, 2)-\u003eunwrap();\n\n// result: [1, 2, 3, 4]\n```\n\n\n@template A\n\n@param A[] $array \n\n@param A[] $values \n\n@return A[]\n\n\n\n\u003ca name=\"values\"\u003e\u003c/a\u003e\n### values\n\nReturn the values of an array as a sequential array.\n\n```php\nuse FriendlyPixel\\Ar\\Ar;\n\n$result = Ar::values([3 =\u003e 'a', 'foo' =\u003e 'b', 1 =\u003e 'c']); \n$result = Ar::wrap([3 =\u003e 'a', 'foo' =\u003e 'b', 1 =\u003e 'c'])-\u003evalues()-\u003eunwrap();\n// result: [0 =\u003e 'a', 1 =\u003e 'b', 2 =\u003e 'c']\n```\n\n\n@template A\n\n@param array\u003cmixed, A\u003e $array \n\n@return array\u003cint, A\u003e\n\n\n\n\n\n\n\n\n\n\n## Fluent style only methods\n\n\n\n\n\u003ca name=\"wrap\"\u003e\u003c/a\u003e\n### wrap\n\nWrap an array, so you can use fluent syntax to call multiple methods on it.\nUse `-\u003eunwrap()` at the end if you need a pure array again.\n\n```php\nuse FriendlyPixel\\Ar\\Ar;\n$numbers = Ar::wrap([1, 2, 3])\n    -\u003emap(function ($value, $key) { return $value * 2; })\n    -\u003efilter(function ($value) { return $value != 6; })\n    -\u003eunwrap()\n;\n\n// If you don't like the Ar::wrap syntax, you can also use ArFluent directly:\nuse FriendlyPixel\\Ar\\ArFluent;\n\n$numbers = (new ArFluent([1, 2, 3]))\n    -\u003emap(function ($value, $key) { return $value * 2; })\n    -\u003efilter(function ($value) { return $value != 6; })\n    -\u003eunwrap()\n;\n\n```\n\n\n\n\u003ca name=\"unwrap\"\u003e\u003c/a\u003e\n### unwrap\n\nReturn the underlying array.\n\n```php\nuse FriendlyPixel\\Ar\\Ar;\n$numbers = Ar::wrap([1, 2, 3])\n    -\u003emap(function ($value, $key) { return $value * 2; })\n    -\u003eunwrap()\n;\n// Result: [2, 4, 6]\n```\n\n\n\n\u003ca name=\"toArray\"\u003e\u003c/a\u003e\n### toArray\n\nAlias for [unwrap()](#unwrap)\n\n\n\n\n## License\n\n[MIT license](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffriendly-pixel%2Far","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffriendly-pixel%2Far","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffriendly-pixel%2Far/lists"}