selective/array-reader

A strictly typed array reader

Maintainers

👁 odan

Package info

github.com/selective-php/array-reader

pkg:composer/selective/array-reader

Statistics

Installs: 72 238

Dependents: 5

Suggesters: 0

Stars: 11

Open Issues: 0

2.3.0 2026-03-07 10:41 UTC

Requires

Suggests

None

Provides

None

Conflicts

None

Replaces

None

MIT 4e74565cc222688b23c2d4d46d8db314cfb42377

arrayreaderstricttypedstrongstrictly

This package is auto-updated.

Last update: 2026-06-07 11:10:07 UTC


README

A strictly typed array reader for PHP.

👁 Latest Version on Packagist
👁 Software License
👁 Build Status
👁 Total Downloads

Requirements

  • PHP 8.2 - 8.5

Installation

composer require selective/array-reader

Usage

You can use the ArrayReader to read single values from a multidimensional array by passing the path to one of the get{type}() and find{type}() methods.

Each get*() / find*() method takes a default value as second argument. If the path cannot be found in the original array, the default is used as return value.

A get*() method returns only the declared return type. If the default value is not given and the element cannot be found, an exception is thrown.

A find*() method returns only the declared return type or null. No exception is thrown if the element cannot be found.

<?php

use Selective\ArrayReader\ArrayReader;

$arrayReader = new ArrayReader([
 'key1' => [
 'key2' => [
 'key3' => 'value1',
 ]
 ]
]);

// Output: value1
echo $arrayReader->getString('key1.key2.key3');

Better Code Quality

Converting complex data with simple PHP works by using a lot of type casting and if conditions etc. This leads to very high cyclomatic complexity and nesting depth, and thus poor "code rating".

Before: Conditions: 10, Paths: 512, CRAP Score: 10

After: Conditions: 1, Paths: 1, CRAP Score: 1

Similar libraries

License

The MIT License (MIT). Please see License File for more information.