khalyomede / array-get
Function to traverse an array using dot and star syntax.
v0.1.2
2018-11-18 08:31 UTC
Requires
- php: >=7.2.0
Requires (Dev)
- phpunit/phpunit: 7.*
This package is auto-updated.
Last update: 2024-11-18 22:11:32 UTC
README
Function to traverse an array using dot and star syntax. Inspired by Laravel Validator class.
$array = [ 'firstname' => 'John', 'lastname' => 'Doe', 'orders' => [ [ 'id' => 36, 'ordered_at' => '2018-11-16 22:12:38', 'product' => [ 'id' => 11, 'name' => 'Huawei P20' ] ], [ 'id' => 47, 'ordered_at' => '2018-11-16 22:13:04', 'product' => [ 'id' => 208, 'name' => 'Powerbank Tesla' ] ] ] ]; var_dump( array_get($array, 'firstname') ); // "John" var_dump( array_get($array, 'orders.1.ordered_at') ); // "2018-11-16 22:13:04" var_dump( array_get($array, 'orders.*.product.name') ) // ["Huawei P20", "Powerbank Tesla"]
Summary
Installation
In your project directory:
composer require khalyomede/array-get:0.*
Examples
- Example 1: get a value from a key
- Example 2: get a value from an indexed key
- Example 3: get values from a nested key
- Example 4: use OOP style
- Example 5: get the key of each items
Example 1: get a value from a key
require __DIR__ . '/../vendor/autoload.php'; use function Khalyomede\array_get; $array = [ 'firstname' => 'John', 'lastname' => 'Doe', 'orders' => [ [ 'id' => 36, 'ordered_at' => '2018-11-16 22:12:38', 'product' => [ 'id' => 11, 'name' => 'Huawei P20' ] ], [ 'id' => 47, 'ordered_at' => '2018-11-16 22:13:04', 'product' => [ 'id' => 208, 'name' => 'Powerbank Tesla' ] ] ] ]; $firstname = array_get($array, 'firstname'); var_dump($firstname); // "John"
Example 2: get a value from an indexed key
require __DIR__ . '/../vendor/autoload.php'; use function Khalyomede\array_get; $array = [ 'firstname' => 'John', 'lastname' => 'Doe', 'orders' => [ [ 'id' => 36, 'ordered_at' => '2018-11-16 22:12:38', 'product' => [ 'id' => 11, 'name' => 'Huawei P20' ] ], [ 'id' => 47, 'ordered_at' => '2018-11-16 22:13:04', 'product' => [ 'id' => 208, 'name' => 'Powerbank Tesla' ] ] ] ]; $ordered_at = array_get($array, 'orders.1.ordered_at'); var_dump($ordered_at); // "2018-11-16 22:13:04"
Example 3: get values from a nested key
require __DIR__ . '/../vendor/autoload.php'; use function Khalyomede\array_get; $array = [ 'firstname' => 'John', 'lastname' => 'Doe', 'orders' => [ [ 'id' => 36, 'ordered_at' => '2018-11-16 22:12:38', 'product' => [ 'id' => 11, 'name' => 'Huawei P20' ] ], [ 'id' => 47, 'ordered_at' => '2018-11-16 22:13:04', 'product' => [ 'id' => 208, 'name' => 'Powerbank Tesla' ] ] ] ]; $ordered_at = array_get($array, 'orders.*.product.name'); var_dump($ordered_at); // ["Huawei P20", "Powerbank Tesla"]
Example 4: use OOP style
require __DIR__ . '/../vendor/autoload.php'; use Khalyomede\Arr; $array = [ 'firstname' => 'John', 'lastname' => 'Doe', 'orders' => [] ]; $firstname = (new Arr($array))->get('firstname'); var_dump($firstname); // "John"
Example 5: get the key of each items
require __DIR__ . '/../vendor/autoload.php'; use function Khalyomede\array_get; $tasks = [ ['id' => 53, 'name' => 'read mails'], ['id' => 61, 'name' => 'factorize the code'], ['id' => 71, 'name' => 'learn ES8 & ES9'] ]; var_dump( array_get($tasks, '*.name') ); // ["read mails", "factorize the code", "learn ES8 & ES9"]