codeliner/array-reader

PHP ArrayReader

Installs: 2 157

Dependents: 7

Stars: 7

Watchers: 1

Forks: 2

Open Issues: 1

Language: PHP

v1.2.0 2015-07-02 17:50 UTC

README

PHP ArrayReader

Build Status

Installation

Installation of codeliner\array-reader uses composer. For composer documentation, please refer to getcomposer.org. Add following requirement to your composer.json

"codeliner/array-reader" : "~1.0"

Usage

You can use the ArrayReader to read single values from a multidimensional array by passing the path to one of the {type}Value() methods. Each {type}Value() method takes a default value as second argument If the path can not be found in the original array, the default is used as return value.

Example

$arrayReader = new ArrayReader(
    array(
        'hash' => array(
            'with' => array(
                'nested' => 'value'
            )
        )
    )
);

echo $arrayReader->stringValue('hash.with.nested'));

//Output: value

$arrayReader = new ArrayReader(
    array(
        'hash' => array(
            'with' => array(
                'nested' => 'value'
            )
        )
    )
);

echo $arrayReader->stringValue('hash.not.existing.path', 'defaultString'));

//Output: defaultString


//If a key in your array contains a dot you escape it in the path with a backslash

$arrayReader = new ArrayReader(
    array(
        'hash' => array(
            'with.dot.key' => array(
                'nested' => 'value'
            )
        )
    )
);

echo $arrayReader->stringValue('hash.with\.dot\.key.nested'));

//Output: value