PHP code example of khalyomede / array-get

1. Go to this page and download the library: Download khalyomede/array-get library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

khalyomede / array-get example snippets


$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"]



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"



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"



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"]



use Khalyomede\Arr;

$array = [
  'firstname' => 'John',
  'lastname' => 'Doe',
  'orders' => []
];

$firstname = (new Arr($array))->get('firstname');

var_dump($firstname); // "John"



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"]
bash
composer