{"id":15433849,"url":"https://github.com/rentalhost/vanilla-array-query","last_synced_at":"2025-03-28T06:12:17.690Z","repository":{"id":62534830,"uuid":"285084710","full_name":"rentalhost/vanilla-array-query","owner":"rentalhost","description":"Extracts array elements into a new array with only queried keys.","archived":false,"fork":false,"pushed_at":"2021-04-24T23:47:03.000Z","size":20,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-18T08:32:01.312Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rentalhost.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-08-04T19:52:58.000Z","updated_at":"2022-05-20T07:59:33.000Z","dependencies_parsed_at":"2022-11-02T15:15:20.945Z","dependency_job_id":null,"html_url":"https://github.com/rentalhost/vanilla-array-query","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rentalhost%2Fvanilla-array-query","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rentalhost%2Fvanilla-array-query/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rentalhost%2Fvanilla-array-query/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rentalhost%2Fvanilla-array-query/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rentalhost","download_url":"https://codeload.github.com/rentalhost/vanilla-array-query/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245978277,"owners_count":20703678,"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":[],"created_at":"2024-10-01T18:35:43.829Z","updated_at":"2025-03-28T06:12:17.674Z","avatar_url":"https://github.com/rentalhost.png","language":"PHP","readme":"# ArrayQuery\n\nA simple way to extract values from an array.\n\n**Real world:** sometimes you need to work with an array with a lot of unnecessary information within a context, and it can be a problem. The `ArrayQuery` package can help simplify the return in a simple way, even with a multidimensional array.\n\n## Installation\n\n### With Composer\n\n```\n$ composer require rentalhost/vanilla-array-query\n```\n\n```json\n{\n    \"require\": {\n        \"rentalhost/vanilla-array-query\": \"^0.1\"\n    }\n}\n```\n\n```php\nrequire 'vendor/autoload.php';\n\nuse Rentalhost\\Vanilla\\ArrayQuery\\ArrayQuery;\n\nArrayQuery::query(\n    [ 'name' =\u003e 'John Doe', 'age' =\u003e 30 ],\n    [ 'age' ]\n) === [ 'age' =\u003e 30 ];\n```\n\n## Usage\n\n### Simple extraction\n\nAs in the example above, you can extract information easily just by specifying the keys you want to extract from the source array.\n\nLet's assume that we have an array containing information about a page, but we want to extract only its route and title, while all other information is irrelevant at the moment.\n\nThe output will capture from the first argument (the source array) only the keys indicated by the second argument (the query array), in the order in which it is requested.\n\n```php\n$page = [ \n    'route'       =\u003e '/home', \n    'title'       =\u003e 'Home', \n    'description' =\u003e 'Initial page',\n];\n\nArrayQuery::query($page, [ 'route', 'title' ]) === [\n    'route' =\u003e '/home', \n    'title' =\u003e 'Home', \n];\n```\n\n### Bidimensional extraction\n\nIt is also possible to extract information from bidimensional array in a simple way.\n\nIn this case, the query key will represent the key to be extracted from the source, and its value in array will represent the keys to be extracted from the second dimension.\n\nSo, let's assume that we have a bidimensional array containing information about a page, and we want to extract only some information related to it, keeping the original structure of the source array.\n\n```php\n$page = [ \n    'header' =\u003e [ \n        'title'       =\u003e 'Home', \n        'description' =\u003e 'Initial page',\n    ],\n];\n\nArrayQuery::query($page, [ 'header' =\u003e [ 'title' ] ]) === [ \n    'header' =\u003e [ \n        'title' =\u003e 'Home', \n    ],\n];\n```\n\n## Multidimensional extraction\n\nAnd going even further, we can extract information from an array even in a multidimensional source.\n\nIn this case, we pass a query containing an array of arrays, and in its keys we indicate what information we want to extract.\n\nSo, supposing that we have an array containing various information from several pages, but we are only interested in obtaining its routes, and nothing more.\n\n```php\n$pages = [\n    [\n        'route' =\u003e '/home',\n        'title' =\u003e 'Home',\n    ],\n    [\n        'route' =\u003e '/admin',\n        'title' =\u003e 'Administrative',\n    ],\n];\n\nArrayQuery::query($pages, [ [ 'route' ] ]) === [\n    [\n        'route' =\u003e '/home',\n    ],\n    [\n        'route' =\u003e '/admin',\n    ],\n];\n```\n\n## Customized extraction\n\nIn some cases, the source array is too complex in a multidimensional way, and sometimes you need to simplify the output for something easier to work with.\n\nFor example, let's say you have a page, and the only thing that interests you is the title. But the title is inside the key header with a lot of information that you don't need in context.\n\nIn this case, you can enter a custom key that has a function as a value. This function will receive all the information existing at that level, and its return will be the output to that new key.\n\nIn the example below, note that the source array does not have a title key, but we will create it on-the-fly from the existing value inside the title key that is inside the header key.\n\n```php\n$page = [ \n    'header' =\u003e [ \n        'title' =\u003e 'Home', \n        'description' =\u003e 'Initial page' \n    ] \n];\n\nArrayQuery::query($page, [ 'title' =\u003e static function (array $page) {\n    return $page['header']['title'];\n} ]) === [ \n    'title' =\u003e 'Home', \n];\n```\n\nThis functionality will also allow you to transform an existing key, filtering its elements, for example.\n\nLet's assume, then, that we have a page with a simplified route, but we want to make it absolute. The key will be kept as a route, but its value will change.\n\nIt is important to remember that the callable will receive the entire array of that context (the `$page`, in this case), and not the value of the key you are reassigning.\n\n```php\n$page = [ 'route' =\u003e '/home' ];\n\nArrayQuery::query($page, [ 'route' =\u003e static function (array $page) {\n    return 'https://...' . $page['route'];\n} ]) === [ \n    'route' =\u003e 'https://.../home', \n];\n```\n\n### Advanced extraction\n\nFinally, it is possible to perform an advanced extraction of values, creating keys on-the-fly through a callable as a value, but without a key.\n\nThe callable must return an array, where the keys that will be applied to the source array.\n\nLet's assume, then, that we have several pages, and we want to use the own route as the key and the title as value.\n\n```php\n$pages = [\n    [\n        'route' =\u003e '/home',\n        'title' =\u003e 'Home'\n    ],\n    [\n        'route' =\u003e '/admin',\n        'title' =\u003e 'Administrative'    \n    ],\n];\n\nArrayQuery::query($pages, [ static function (array $page) {\n    return array_combine(\n        array_column($page, 'route'),\n        array_column($page, 'title'),\n    );\n} ]) === [ \n    '/home'  =\u003e 'Home',\n    '/admin' =\u003e 'Administrative'\n];\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frentalhost%2Fvanilla-array-query","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frentalhost%2Fvanilla-array-query","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frentalhost%2Fvanilla-array-query/lists"}