{"id":18656265,"url":"https://github.com/bayfrontmedia/php-array-helpers","last_synced_at":"2025-10-05T01:42:50.613Z","repository":{"id":53790848,"uuid":"282979438","full_name":"bayfrontmedia/php-array-helpers","owner":"bayfrontmedia","description":"PHP helper class to provide useful array functions.","archived":false,"fork":false,"pushed_at":"2024-12-23T21:06:44.000Z","size":35,"stargazers_count":14,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-25T16:51:31.318Z","etag":null,"topics":["array","function","helper","helpers","php"],"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/bayfrontmedia.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":".github/SECURITY.md","support":null}},"created_at":"2020-07-27T18:15:19.000Z","updated_at":"2024-12-23T21:06:27.000Z","dependencies_parsed_at":"2023-02-18T10:16:55.750Z","dependency_job_id":null,"html_url":"https://github.com/bayfrontmedia/php-array-helpers","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bayfrontmedia%2Fphp-array-helpers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bayfrontmedia%2Fphp-array-helpers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bayfrontmedia%2Fphp-array-helpers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bayfrontmedia%2Fphp-array-helpers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bayfrontmedia","download_url":"https://codeload.github.com/bayfrontmedia/php-array-helpers/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248449900,"owners_count":21105582,"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","function","helper","helpers","php"],"created_at":"2024-11-07T07:22:41.795Z","updated_at":"2025-10-05T01:42:50.586Z","avatar_url":"https://github.com/bayfrontmedia.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"## PHP array helpers\n\nPHP helper class to provide useful array functions.\n\n- [License](#license)\n- [Author](#author)\n- [Requirements](#requirements)\n- [Installation](#installation)\n- [Usage](#usage)\n\n## License\n\nThis project is open source and available under the [MIT License](LICENSE).\n\n## Author\n\n\u003cimg src=\"https://cdn1.onbayfront.com/bfm/brand/bfm-logo.svg\" alt=\"Bayfront Media\" width=\"250\" /\u003e\n\n- [Bayfront Media homepage](https://www.bayfrontmedia.com?utm_source=github\u0026amp;utm_medium=direct)\n- [Bayfront Media GitHub](https://github.com/bayfrontmedia)\n\n## Requirements\n\n* PHP `^8.0` (Tested up to `8.4`)\n\n## Installation\n\n```\ncomposer require bayfrontmedia/php-array-helpers\n```\n\n## Usage\n\n- [dot](#dot)\n- [undot](#undot)\n- [set](#set)\n- [has](#has)\n- [get](#get)\n- [pluck](#pluck)\n- [forget](#forget)\n- [except](#except)\n- [exceptValues](#exceptvalues)\n- [only](#only)\n- [missing](#missing)\n- [isMissing](#ismissing)\n- [multisort](#multisort)\n- [numericMultisort](#numericmultisort)\n- [renameKeys](#renamekeys)\n- [order](#order)\n- [getRandomItems](#getrandomitems)\n- [query](#query)\n- [getAnyValues](#getanyvalues)\n- [hasAnyValues](#hasanyvalues)\n- [hasAllValues](#hasallvalues)\n- [ensureHas](#ensurehas)\n\n\u003chr /\u003e\n\n### dot\n\n**Description:**\n\nConverts a multidimensional array to a single depth \"dot\" notation array, optionally prepending a string to each array key.\n\nThe key values will never be an array, even if empty. Empty arrays will be dropped.\n\n**Parameters:**\n\n- `$array` (array): Original array\n- `$prepend = ''` (string): String to prepend\n\n**Returns:**\n\n- (array)\n\n**Example:**\n\n```\n$array = [\n    'name' =\u003e [\n        'first_name' =\u003e 'John',\n        'last_name' =\u003e 'Doe'\n    ],\n    'hobbies' =\u003e [ // This empty array will be dropped\n\n    ]\n];\n\n$dot = Arr::dot($array);\n```\n\n\u003chr /\u003e\n\n### undot\n\n**Description:**\n\nConverts array in \"dot\" notation to a standard multidimensional array.\n\n**Parameters:**\n\n- `$array` (array): Array in \"dot\" notation\n\n**Returns:**\n\n- (array)\n\n**Example:**\n\n```\n$array = [\n    'name.first_name' =\u003e 'John',\n    'name.last_name' =\u003e 'Doe'\n];\n\n$undot = Arr::undot($array);\n```\n\n\u003chr /\u003e\n\n### set\n\n**Description:**\n\nSet an array item to a given value using \"dot\" notation.\n\n**Parameters:**\n\n- `$array` (array): Original array\n- `$key` (string): Key to set in \"dot\" notation\n- `$value` (mixed): Value of key\n\n**Returns:**\n\n- (void)\n\n**Example:**\n\n```\n$array = [\n    'name' =\u003e [\n        'first_name' =\u003e 'John',\n        'last_name' =\u003e 'Doe'\n    ],\n];\n\nArr::set($array, 'name.middle_name', 'Middle');\n```\n\n\u003chr /\u003e\n\n### has\n\n**Description:**\n\nChecks if array key exists and not null using \"dot\" notation.\n\n**Parameters:**\n\n- `$array` (array): Original array\n- `$key` (string): Key to check in \"dot\" notation\n\n**Returns:**\n\n- (bool)\n\n**Example:**\n\n```\n$array = [\n    'name' =\u003e [\n        'first_name' =\u003e 'John',\n        'last_name' =\u003e 'Doe'\n    ],\n];\n\nif (Arr::has($array, 'name.first_name')) {\n    // Do something\n}\n```\n\n\u003chr /\u003e\n\n### get\n\n**Description:**\n\nGet an item from an array using \"dot\" notation, returning an optional default value if not found.\n\n**Parameters:**\n\n- `$array` (array): Original array\n- `$key` (string): Key to return in \"dot\" notation\n- `$default = NULL` (mixed): Default value to return\n\n**Returns:**\n\n- (mixed)\n\n**Example:**\n\n```\n$array = [\n    'name' =\u003e [\n        'first_name' =\u003e 'John',\n        'last_name' =\u003e 'Doe'\n    ],\n];\n\necho Arr::get($array, 'name.first_name');\n```\n\n\u003chr /\u003e\n\n### pluck\n\n**Description:**\n\nReturns an array of values for a given key from an array using \"dot\" notation.\n\n**Parameters:**\n\n- `$array` (array): Original array\n- `$value` (string): Value to return in \"dot\" notation\n- `$key = NULL` (string|null): Optionally how to key the returned array in \"dot\" notation\n\n**Returns:**\n\n- (array)\n\n**Example:**\n\n```\n$array = [\n    [\n        'user_id' =\u003e 110,\n        'username' =\u003e 'John',\n        'active' =\u003e true\n    ],\n    [\n        'user_id' =\u003e 111,\n        'username' =\u003e 'Jane',\n        'active' =\u003e true\n    ]\n];\n\n$array = Arr::pluck($array, 'username', 'user_id');\n```\n\n\u003chr /\u003e\n\n### forget\n\n**Description:**\n\nRemove a single key, or an array of keys from a given array using \"dot\" notation.\n\n**Parameters:**\n\n- `$array` (array): Original array\n- `$keys` (string|array): Key(s) to forget in \"dot\" notation\n\n**Returns:**\n\n- (void)\n\n**Example:**\n\n```\n$array = [\n    'name' =\u003e [\n        'first_name' =\u003e 'John',\n        'last_name' =\u003e 'Doe'\n    ],\n];\n\nArr::forget($array, 'name.last_name');\n```\n\n\u003chr /\u003e\n\n### except\n\n**Description:**\n\nReturns the original array except given key(s).\n\n**Parameters:**\n\n- `$array` (array): Original array\n- `$keys` (string|array): Key(s) to remove\n\n**Returns:**\n\n- (array)\n\n**Example:**\n\n```\n$array = [\n    'user_id' =\u003e 110,\n    'username' =\u003e 'John',\n    'active' =\u003e true\n];\n\n$array = Arr::except($array, 'active');\n```\n\n\u003chr /\u003e\n\n### exceptValues\n\n**Description:**\n\nReturns the original array except given value(s).\n\n**Parameters:**\n\n- `$array` (array): Original array\n- `$values` (string|array): Values(s) to remove\n\n**Returns:**\n\n- (array)\n\n**Example:**\n\n```\n$array = [\n    'John',\n    'Jane',\n    'Bob'\n];\n\n$array = Arr::exceptValues($array, 'John');\n```\n\n\u003chr /\u003e\n\n### only\n\n**Description:**\n\nReturns only desired key(s) from an array.\n\n**Parameters:**\n\n- `$array` (array): Original array\n- `$keys` (string|array): Key(s) to return\n\n**Returns:**\n\n- (array)\n\n**Example:**\n\n```\n$array = [\n    'user_id' =\u003e 110,\n    'username' =\u003e 'John',\n    'active' =\u003e true\n];\n\n$array = Arr::only($array, 'username');\n```\n\n\u003chr /\u003e\n\n### missing\n\n**Description:**\n\nReturns array of missing keys from the original array, or an empty array if none are missing.\n\n**Parameters:**\n\n- `$array` (array): Original array\n- `$keys` (array): Key(s) to check\n\n**Returns:**\n\n- (array)\n\n**Example:**\n\n```\n$array = [\n    'user_id' =\u003e 110,\n    'username' =\u003e 'John',\n    'active' =\u003e true\n];\n\n$missing = Arr::missing($array, [\n    'active',\n    'last_login'\n]);\n```\n\n\u003chr /\u003e\n\n### isMissing\n\n**Description:**\n\nChecks if keys are missing from the original array.\n\n**Parameters:**\n\n- `$array` (array): Original array\n- `$keys` (array): Key(s) to check\n\n**Returns:**\n\n- (bool)\n\n**Example:**\n\n```\n$array = [\n    'user_id' =\u003e 110,\n    'username' =\u003e 'John',\n    'active' =\u003e true\n];\n\nif (Arr::isMissing($array, [\n    'active',\n    'last_login'\n])) {\n    // Do something\n}\n```\n\n\u003chr /\u003e\n\n### multisort\n\n**Description:**\n\nSort a multidimensional array by a given key in ascending (optionally, descending) order.\n\n**Parameters:**\n\n- `$array` (array): Original array\n- `$key` (string): Key name to sort by\n- `$descending = false` (bool): Sort descending\n\n**Returns:**\n\n- (array)\n\n**Example:**\n\n```\n$clients = [\n    [\n        'first_name' =\u003e 'John',\n        'last_name' =\u003e 'Doe'\n    ],\n    [\n        'first_name' =\u003e 'Jane',\n        'last_name' =\u003e 'Doe'\n    ]\n];\n\n$sorted = Arr::multisort($clients, 'first_name');\n```\n\n\u003chr /\u003e\n\n### numericMultisort\n\n**Description:**\n\nSort a numerically indexed array of multidimensional arrays by a given key in ascending (optionally, descending) order.\n\n**Parameters:**\n\n- `$array` (array): Numerically indexed array of multidimensional arrays\n- `$key` (string): Key name to sort by in dot notation\n- `$descending = false` (bool): Sort descending\n\n**Returns:**\n\n- (array)\n\n**Example:**\n\n```\n$contacts = [\n    [\n        'id' =\u003e 1,\n        'contact' =\u003e [\n            'name' =\u003e 'James'\n        ]\n    ],\n    [\n        'id' =\u003e 2,\n        'contact' =\u003e [\n            'name' =\u003e 'Bob'\n        ]\n    ],\n    [\n        'id' =\u003e 3,\n        'contact' =\u003e [\n            'name' =\u003e 'Carl'\n        ]\n    ]\n];\n\n$sorted = Arr::numericMultisort($contacts, 'contact.name');\n```\n\n\u003chr /\u003e\n\n### renameKeys\n\n**Description:**\n\nRename array keys while preserving their order.\n\n**Parameters:**\n\n- `$array` (array): Original array\n- `$keys` (array): Key/value pairs to rename\n\n**Returns:**\n\n- (array)\n\n**Example:**\n\n```\n$user = [\n    'UserID' =\u003e 5,\n    'UserEmail' =\u003e 'name@example.com',\n    'UserGroup' =\u003e 'Administrator'\n];\n\n$renamed = Arr::renameKeys($user, [\n    'UserID' =\u003e 'id',\n    'UserEmail' =\u003e 'email',\n    'UserGroup' =\u003e 'group'\n]);\n```\n\n\u003chr /\u003e\n\n### order\n\n**Description:**\n\nOrder an array based on an array of keys.\n\nKeys from the `$order` array which do not exist in the original array will be ignored.\n\n**Parameters:**\n\n- `$array` (array): Original array\n- `$order` (array): Array of keys in the order to be returned\n\n**Returns:**\n\n- (array)\n\n**Example:**\n\n```\n$address = [\n    'street' =\u003e '123 Main St.',\n    'state' =\u003e 'IL',\n    'zip' =\u003e '60007',\n    'city' =\u003e 'Chicago'\n];\n\n$order = [\n    'street',\n    'city',\n    'state',\n    'zip',\n    'country'\n];\n\n$address = Arr::order($address, $order);\n```\n\nThe above example will return the following array:\n\n```\nArray\n(\n    [street] =\u003e 123 Main St.\n    [city] =\u003e Chicago\n    [state] =\u003e IL\n    [zip] =\u003e 60007\n)\n```\n\n\u003chr /\u003e\n\n### getRandomItems\n\n**Description:**\n\nGet random items from array.\n\nIf `$count` is greater than or equal to the number of items on the array,\nthe original array is returned in random order.\n\n**Parameters:**\n\n- `$array` (array): Original array\n- `$count` (int): Number of random items to return\n\n**Returns:**\n\n- (array)\n\n**Example:**\n\n```\n$array = [\n    'John',\n    'Jane',\n    'Bob'\n];\n\n$array = Arr::getRandomItems($array, 1);\n```\n\n\u003chr /\u003e\n\n### query\n\n**Description:**\n\nConvert array into a query string.\n\n**Parameters:**\n\n- `$array` (array): Original array\n\n**Returns:**\n\n- (string)\n\n**Example:**\n\n```\n$array = [\n    'first_name' =\u003e 'Jane',\n    'last_name' =\u003e 'Doe'\n];\n\necho Arr::query($array);\n```\n\n\u003chr /\u003e\n\n### getAnyValues\n\n**Description:**\n\nReturn an array of values which exist in a given array.\n\n**Parameters:**\n\n- `$array` (array)\n- `$values` (array)\n\n**Returns:**\n\n- (array)\n\n**Example:**\n\n```\n$array = [\n    'name' =\u003e [\n        'John',\n        'Dave'\n    ],\n];\n\n$existing_values = Arr::getAnyValues($array['name'], [\n    'John',\n    'Jane'\n]);\n```\n\n\u003chr /\u003e\n\n### hasAnyValues\n\n**Description:**\n\nDo any values exist in a given array.\n\n**Parameters:**\n\n- `$array` (array)\n- `$values` (array)\n\n**Returns:**\n\n- (bool)\n\n**Example:**\n\n```\n$array = [\n    'name' =\u003e [\n        'John',\n        'Dave'\n    ],\n];\n\nif (Arr::hasAnyValues($array['name'], [\n    'John',\n    'Jane'\n])) { \n     // Do something\n}\n```\n\n\u003chr /\u003e\n\n### hasAllValues\n\n**Description:**\n\nDo all values exist in a given array.\n\n**Parameters:**\n\n- `$array` (array)\n- `$values` (array)\n\n**Returns:**\n\n- (bool)\n\n**Example:**\n\n```\n$array = [\n    'name' =\u003e [\n        'John',\n        'Dave'\n    ],\n];\n\nif (Arr::hasAllValues($array['name'], [\n    'John',\n    'Jane'\n])) { \n     // Do something\n}\n```\n\n\u003chr /\u003e\n\n### ensureHas\n\n**Description:**\n\nEnsure a numerically indexed array of arrays has a given item based on a unique key.\n\n**Parameters:**\n\n- `$array` (array)\n- `$item` (array): Item to exist\n- `$unique_key` (string): Unique array key\n\n**Returns:**\n\n- (bool)\n\n**Example:**\n\n```\n$contacts = [\n    [\n        'id' =\u003e 1,\n        'contact' =\u003e [\n            'name' =\u003e 'James'\n        ]\n    ],\n    [\n        'id' =\u003e 2,\n        'contact' =\u003e [\n            'name' =\u003e 'Bob'\n        ]\n    ],\n    [\n        'id' =\u003e 3,\n        'contact' =\u003e [\n            'name' =\u003e 'Carl'\n        ]\n    ]\n];\n\n$item = [\n    'id' =\u003e 2,\n    'contact' =\u003e [\n        'name' =\u003e 'Bob'\n    ]\n];\n\n$contacts = Arr::ensureHas($contacts, $item, 'id');\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbayfrontmedia%2Fphp-array-helpers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbayfrontmedia%2Fphp-array-helpers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbayfrontmedia%2Fphp-array-helpers/lists"}