{"id":19357733,"url":"https://github.com/joomla-framework/utilities","last_synced_at":"2025-04-06T04:15:46.567Z","repository":{"id":7094623,"uuid":"8385918","full_name":"joomla-framework/utilities","owner":"joomla-framework","description":"Joomla Framework Utilities Package","archived":false,"fork":false,"pushed_at":"2025-03-29T21:12:01.000Z","size":236,"stargazers_count":4,"open_issues_count":4,"forks_count":11,"subscribers_count":11,"default_branch":"3.x-dev","last_synced_at":"2025-03-29T22:22:05.547Z","etag":null,"topics":["array-helper","joomla","joomla-framework","php","utilities"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/joomla-framework.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"joomla","custom":"https://community.joomla.org/sponsorship-campaigns.html"}},"created_at":"2013-02-24T03:37:06.000Z","updated_at":"2025-03-29T21:12:04.000Z","dependencies_parsed_at":"2025-01-06T11:26:04.905Z","dependency_job_id":"09f3af7e-d608-4dcf-b582-c583f6386b4c","html_url":"https://github.com/joomla-framework/utilities","commit_stats":{"total_commits":171,"total_committers":19,"mean_commits":9.0,"dds":0.6491228070175439,"last_synced_commit":"3a19dda4fb7b368bb028e421509e501664e0f505"},"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joomla-framework%2Futilities","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joomla-framework%2Futilities/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joomla-framework%2Futilities/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joomla-framework%2Futilities/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joomla-framework","download_url":"https://codeload.github.com/joomla-framework/utilities/tar.gz/refs/heads/3.x-dev","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247430964,"owners_count":20937875,"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-helper","joomla","joomla-framework","php","utilities"],"created_at":"2024-11-10T07:09:00.758Z","updated_at":"2025-04-06T04:15:46.532Z","avatar_url":"https://github.com/joomla-framework.png","language":"PHP","funding_links":["https://github.com/sponsors/joomla","https://community.joomla.org/sponsorship-campaigns.html"],"categories":[],"sub_categories":[],"readme":"# The Utilities Package [![Build Status](https://ci.joomla.org/api/badges/joomla-framework/utilities/status.svg?ref=refs/heads/3.x-dev)](https://ci.joomla.org/joomla-framework/utilities)\r\n\r\n[![Latest Stable Version](https://poser.pugx.org/joomla/utilities/v/stable)](https://packagist.org/packages/joomla/utilities)\r\n[![Total Downloads](https://poser.pugx.org/joomla/utilities/downloads)](https://packagist.org/packages/joomla/utilities)\r\n[![Latest Unstable Version](https://poser.pugx.org/joomla/utilities/v/unstable)](https://packagist.org/packages/joomla/utilities)\r\n[![License](https://poser.pugx.org/joomla/utilities/license)](https://packagist.org/packages/joomla/utilities)\r\n\r\n## Using ArrayHelper\r\n\r\n### toInteger\r\n\r\n```php\r\nuse Joomla\\Utilities\\ArrayHelper;\r\n\r\n$input = array(\r\n    \"width\" =\u003e \"100\",\r\n    \"height\" =\u003e \"200xxx\",\r\n    \"length\" =\u003e \"10.3\"\r\n);\r\n$result = ArrayHelper::toInteger($input);\r\nvar_dump($result);\r\n```\r\nResult:\r\n```\r\narray(3) {\r\n  'width' =\u003e\r\n  int(100)\r\n  'height' =\u003e\r\n  int(200)\r\n  'length' =\u003e\r\n  int(10)\r\n}\r\n```\r\n\r\n### toObject\r\n\r\n```php\r\nuse Joomla\\Utilities\\ArrayHelper;\r\n\r\nclass Book {\r\n    public $name;\r\n    public $author;\r\n    public $genre;\r\n    public $rating;\r\n}\r\nclass Author {\r\n    public $name;\r\n    public $born;\r\n}\r\n$input = array(\r\n    \"name\" =\u003e \"The Hitchhiker's Guide to the Galaxy\",\r\n    \"author\" =\u003e array(\r\n        \"name\" =\u003e \"Douglas Adams\",\r\n        \"born\" =\u003e 1952,\r\n        \"died\" =\u003e 2001),\r\n    \"genre\" =\u003e \"comic science fiction\",\r\n    \"rating\" =\u003e 10\r\n);\r\n$book = ArrayHelper::toObject($input, 'Book');\r\nvar_dump($book);\r\n```\r\nResult:\r\n```\r\nclass Book#1 (4) {\r\n  public $name =\u003e\r\n  string(36) \"The Hitchhiker's Guide to the Galaxy\"\r\n  public $author =\u003e\r\n  class Book#2 (6) {\r\n    public $name =\u003e\r\n    string(13) \"Douglas Adams\"\r\n    public $author =\u003e\r\n    NULL\r\n    public $genre =\u003e\r\n    NULL\r\n    public $rating =\u003e\r\n    NULL\r\n    public $born =\u003e\r\n    int(1952)\r\n    public $died =\u003e\r\n    int(2001)\r\n  }\r\n  public $genre =\u003e\r\n  string(21) \"comic science fiction\"\r\n  public $rating =\u003e\r\n  int(10)\r\n}\r\n```\r\n\r\n### toString\r\n\r\n```php\r\nuse Joomla\\Utilities\\ArrayHelper;\r\n\r\n$input = array(\r\n    \"fruit\" =\u003e \"apple\",\r\n    \"pi\" =\u003e 3.14\r\n);\r\necho ArrayHelper::toString($input);\r\n```\r\nResult:\r\n```\r\nfruit=\"apple\" pi=\"3.14\"\r\n```\r\n\r\n### fromObject\r\n\r\n```php\r\nuse Joomla\\Utilities\\ArrayHelper;\r\n\r\nclass Book {\r\n    public $name;\r\n    public $author;\r\n    public $genre;\r\n    public $rating;\r\n}\r\nclass Author {\r\n    public $name;\r\n    public $born;\r\n}\r\n\r\n$book = new Book();\r\n$book-\u003ename = \"Harry Potter and the Philosopher's Stone\";\r\n$book-\u003eauthor = new Author();\r\n$book-\u003eauthor-\u003ename = \"J.K. Rowling\";\r\n$book-\u003eauthor-\u003eborn = 1965;\r\n$book-\u003egenre = \"fantasy\";\r\n$book-\u003erating = 10;\r\n\r\n$array = ArrayHelper::fromObject($book);\r\nvar_dump($array);\r\n```\r\nResult:\r\n```\r\narray(4) {\r\n  'name' =\u003e\r\n  string(40) \"Harry Potter and the Philosopher's Stone\"\r\n  'author' =\u003e\r\n  array(2) {\r\n    'name' =\u003e\r\n    string(12) \"J.K. Rowling\"\r\n    'born' =\u003e\r\n    int(1965)\r\n  }\r\n  'genre' =\u003e\r\n  string(7) \"fantasy\"\r\n  'rating' =\u003e\r\n  int(10)\r\n}\r\n```\r\n\r\n### getColumn\r\n\r\n```php\r\nuse Joomla\\Utilities\\ArrayHelper;\r\n\r\n$rows = array(\r\n    array(\"name\" =\u003e \"John\", \"age\" =\u003e 20),\r\n    array(\"name\" =\u003e \"Alex\", \"age\" =\u003e 35),\r\n    array(\"name\" =\u003e \"Sarah\", \"age\" =\u003e 27)\r\n);\r\n$names = ArrayHelper::getColumn($rows, 'name');\r\nvar_dump($names);\r\n```\r\nResult:\r\n```\r\narray(3) {\r\n  [0] =\u003e\r\n  string(4) \"John\"\r\n  [1] =\u003e\r\n  string(4) \"Alex\"\r\n  [2] =\u003e\r\n  string(5) \"Sarah\"\r\n}\r\n```\r\n\r\n### getValue\r\n```php\r\nuse Joomla\\Utilities\\ArrayHelper;\r\n\r\n$city = array(\r\n    \"name\" =\u003e \"Oslo\",\r\n    \"country\" =\u003e \"Norway\"\r\n);\r\n\r\n// Prints 'Oslo'\r\necho ArrayHelper::getValue($city, 'name');\r\n\r\n// Prints 'unknown mayor' (no 'mayor' key is found in the array)\r\necho ArrayHelper::getValue($city, 'mayor', 'unknown mayor');\r\n```\r\n\r\n### invert\r\n\r\n```php\r\nuse Joomla\\Utilities\\ArrayHelper;\r\n\r\n$input = array(\r\n    'New' =\u003e array('1000', '1500', '1750'),\r\n    'Used' =\u003e array('3000', '4000', '5000', '6000')\r\n);\r\n$output = ArrayHelper::invert($input);\r\nvar_dump($output);\r\n```\r\nResult:\r\n```\r\narray(7) {\r\n  [1000] =\u003e\r\n  string(3) \"New\"\r\n  [1500] =\u003e\r\n  string(3) \"New\"\r\n  [1750] =\u003e\r\n  string(3) \"New\"\r\n  [3000] =\u003e\r\n  string(4) \"Used\"\r\n  [4000] =\u003e\r\n  string(4) \"Used\"\r\n  [5000] =\u003e\r\n  string(4) \"Used\"\r\n  [6000] =\u003e\r\n  string(4) \"Used\"\r\n}\r\n```\r\n\r\n\r\n### isAssociative\r\n\r\n```php\r\nuse Joomla\\Utilities\\ArrayHelper;\r\n\r\n$user = array(\"id\" =\u003e 46, \"name\" =\u003e \"John\");\r\necho ArrayHelper::isAssociative($user) ? 'true' : 'false'; // true\r\n\r\n$letters = array(\"a\", \"b\", \"c\");\r\necho ArrayHelper::isAssociative($letters) ? 'true' : 'false'; // false\r\n```\r\n\r\n### pivot\r\n\r\n```php\r\nuse Joomla\\Utilities\\ArrayHelper;\r\n\r\n$movies = array(\r\n    array('year' =\u003e 1972, 'title' =\u003e 'The Godfather'),\r\n    array('year' =\u003e 2000, 'title' =\u003e 'Gladiator'),\r\n    array('year' =\u003e 2000, 'title' =\u003e 'Memento'),\r\n    array('year' =\u003e 1964, 'title' =\u003e 'Dr. Strangelove')\r\n);\r\n$pivoted = ArrayHelper::pivot($movies, 'year');\r\nvar_dump($pivoted);\r\n```\r\nResult:\r\n```\r\narray(3) {\r\n  [1972] =\u003e\r\n  array(2) {\r\n    'year' =\u003e\r\n    int(1972)\r\n    'title' =\u003e\r\n    string(13) \"The Godfather\"\r\n  }\r\n  [2000] =\u003e\r\n  array(2) {\r\n    [0] =\u003e\r\n    array(2) {\r\n      'year' =\u003e\r\n      int(2000)\r\n      'title' =\u003e\r\n      string(9) \"Gladiator\"\r\n    }\r\n    [1] =\u003e\r\n    array(2) {\r\n      'year' =\u003e\r\n      int(2000)\r\n      'title' =\u003e\r\n      string(7) \"Memento\"\r\n    }\r\n  }\r\n  [1964] =\u003e\r\n  array(2) {\r\n    'year' =\u003e\r\n    int(1964)\r\n    'title' =\u003e\r\n    string(15) \"Dr. Strangelove\"\r\n  }\r\n}\r\n```\r\n\r\n### sortObjects\r\n\r\n```php\r\nuse Joomla\\Utilities\\ArrayHelper;\r\n\r\n$members = array(\r\n    (object) array('first_name' =\u003e 'Carl', 'last_name' =\u003e 'Hopkins'),\r\n    (object) array('first_name' =\u003e 'Lisa', 'last_name' =\u003e 'Smith'),\r\n    (object) array('first_name' =\u003e 'Julia', 'last_name' =\u003e 'Adams')\r\n);\r\n$sorted = ArrayHelper::sortObjects($members, 'last_name', 1);\r\nvar_dump($sorted);\r\n```\r\nResult:\r\n```\r\narray(3) {\r\n  [0] =\u003e\r\n  class stdClass#3 (2) {\r\n    public $first_name =\u003e\r\n    string(5) \"Julia\"\r\n    public $last_name =\u003e\r\n    string(5) \"Adams\"\r\n  }\r\n  [1] =\u003e\r\n  class stdClass#1 (2) {\r\n    public $first_name =\u003e\r\n    string(4) \"Carl\"\r\n    public $last_name =\u003e\r\n    string(7) \"Hopkins\"\r\n  }\r\n  [2] =\u003e\r\n  class stdClass#2 (2) {\r\n    public $first_name =\u003e\r\n    string(4) \"Lisa\"\r\n    public $last_name =\u003e\r\n    string(5) \"Smith\"\r\n  }\r\n}\r\n```\r\n\r\n### arrayUnique\r\n```php\r\nuse Joomla\\Utilities\\ArrayHelper;\r\n\r\n$names = array(\r\n    array(\"first_name\" =\u003e \"John\", \"last_name\" =\u003e \"Adams\"),\r\n    array(\"first_name\" =\u003e \"John\", \"last_name\" =\u003e \"Adams\"),\r\n    array(\"first_name\" =\u003e \"John\", \"last_name\" =\u003e \"Smith\"),\r\n    array(\"first_name\" =\u003e \"Sam\", \"last_name\" =\u003e \"Smith\")\r\n);\r\n$unique = ArrayHelper::arrayUnique($names);\r\nvar_dump($unique);\r\n```\r\nResult:\r\n```\r\narray(3) {\r\n  [0] =\u003e\r\n  array(2) {\r\n    'first_name' =\u003e\r\n    string(4) \"John\"\r\n    'last_name' =\u003e\r\n    string(5) \"Adams\"\r\n  }\r\n  [2] =\u003e\r\n  array(2) {\r\n    'first_name' =\u003e\r\n    string(4) \"John\"\r\n    'last_name' =\u003e\r\n    string(5) \"Smith\"\r\n  }\r\n  [3] =\u003e\r\n  array(2) {\r\n    'first_name' =\u003e\r\n    string(3) \"Sam\"\r\n    'last_name' =\u003e\r\n    string(5) \"Smith\"\r\n  }\r\n}\r\n```\r\n\r\n### flatten\r\n\r\n```php\r\nuse Joomla\\Utilities\\ArrayHelper;\r\n\r\n$array = array(\r\n    'flower' =\u003e array(\r\n        'sakura' =\u003e 'samurai',\r\n        'olive' =\u003e 'peace'\r\n    )\r\n);\r\n\r\n// Flatten the nested array and separate the keys by a dot (\".\")\r\n$flattenend1 = ArrayHelper::flatten($array);\r\n\r\necho $flattenend1['flower.sakura']; // 'samurai'\r\n\r\n// Custom separator\r\n$flattenend2 = ArrayHelper::flatten($array, '/');\r\n\r\necho $flattenend2['flower/olive']; // 'peace'\r\n```\r\n\r\n## Installation via Composer\r\n\r\nAdd `\"joomla/utilities\": \"~3.0\"` to the require block in your composer.json and then run `composer install`.\r\n\r\n```json\r\n{\r\n\t\"require\": {\r\n\t\t\"joomla/utilities\": \"~3.0\"\r\n\t}\r\n}\r\n```\r\n\r\nAlternatively, you can simply run the following from the command line:\r\n\r\n```sh\r\ncomposer require joomla/utilities \"~3.0\"\r\n```\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoomla-framework%2Futilities","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoomla-framework%2Futilities","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoomla-framework%2Futilities/lists"}