码行器 / 数组读取器
PHP 数组读取器
v2.1
2018-12-01 16:53 UTC
Requires
- php: >=7.1
Requires (Dev)
- phpunit/phpunit: >=7.0
This package is not auto-updated.
Last update: 2024-09-14 15:16:30 UTC
README
PHP 数组读取器
安装
码行器\数组读取器的安装使用 composer。有关 composer 文档,请参阅 getcomposer.org。在您的 composer.json 中添加以下需求
"codeliner/array-reader" : "~2.0"
使用方法
您可以通过传递到其中一个 {type}Value()
方法的路径来使用 ArrayReader 从多维数组中读取单个值。每个 {type}Value()
方法接受一个默认值作为第二个参数。如果路径在原始数组中找不到,则使用默认值作为返回值。
示例
$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 //If you need to differentiate between a NULL value and a not existing path, you can explicity check if the path exists: $arrayReader = new ArrayReader( array( 'hash' => array( 'with' => array( 'nested' => null ) ) ) ); if($arrayReader->pathExists('hash.with.nested')) { echo "path exists"; } //Output: path exists