william-lindner/delimiter_array_access

An accessor function for arrays that allows you to find nested values by delimited notation.

2.0.0 2021-04-09 22:12 UTC

This package is auto-updated.

Last update: 2024-04-23 08:40:04 UTC


README

Delimiter array access is a function that allows for the finding of array values by dot notation.

Sometimes it's just easier to read accessing array chains by a string with a delimiter. Instead of compound, chained accessors that may lead to unexpected errors this function is null safe. This isn't trying to be fast. It's trying to provide developers options in how they want their code read.

Descripton

delimiter_array_access( string $needle, array $haystack, [ string $delimiter = '.' ] ) : mixed

Installation

$ composer require william-lindner/delimiter_array_access

Parameters

needle (Required)

The string with delimiter you want parsed to extract the value from the array.

haystack (Required)

The array being parsed to find the value.

delimiter (Optional)

The optional delimiter for the string needle, defaulting to a period.

Example

In the following example the value nested within the array is extracted using the default delimiter of a period.

$myArray = [
  'my' => [
    'assoc' => [
      'array' => 'I found a value.'
    ]
  ]
];

// This will output 'I found a value.'
echo delimiter_array_access('my.assoc.array', $myArray) . PHP_EOL;