3. Getting Values

The get() method allows accessing positions in the object.

Consider the object below for all the examples on this page:

use Cajudev\Collection;

$collection = new Collection([
    'lorem',
    'ipsum' => 'consectetur',
    'dolor' => ['sit' => 'amet']
 ]);

3.1 Simple access

// ================================================================================================================ //

    $collection->get(0); // lorem

// ================================================================================================================ //

3.2 Chained access

// ================================================================================================================ //

    $collection->get('dolor')->get('sit'); // amet

// ================================================================================================================ //

3.3 Access multiple values

// ================================================================================================================ //

    $collection->get('ipsum', 'dolor'); // ['ipsum' => 'consectetur', 'dolor' => ['sit' => 'amet']]

// ================================================================================================================ //

3.4 Multidimensional access using dot notation

// ================================================================================================================ //

    $collection->get('dolor.sit'); // amet

// ================================================================================================================ //

3.5 Attempting to access an invalid key

// ================================================================================================================ //

    $collection->get('invalid'); // null (don't worry no errors will be generated)

// ================================================================================================================ //

3.6 Access all content

// ================================================================================================================ //

    $collection->get(); // ['lorem', 'ipsum', 'dolor' => ['sit' => 'amet']]

// ================================================================================================================ //