Skip to content

A function to find a dataset in an associative array based on dot separated string.

License

Notifications You must be signed in to change notification settings

william-lindner/delimiter_array_access

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Delimiter Array Access

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;

About

A function to find a dataset in an associative array based on dot separated string.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages