Library solves routine actions like:
- Get array value by key/index if exists or return default value
- Set array key/index if not exists
PHP >= 5.0 (tests for 7.1 up to nightly)
composer require io-developer/php-keyget
or composer.json:
"require": {
"io-developer/php-keyget": "*"
}
<?php
$arr = ['foo' => 'bar'];
var_dump([
key_get($arr, 'foo'), // 'bar'
key_get($arr, 'baz'), // null
key_get($arr, 'baz', 'Ooops!'), // 'Ooops!'
]);
<?php
$arr = ['foo' => 'bar'];
key_setdefault($arr, 'foo', 'meow');
var_dump($arr); // no changes
key_setdefault($arr, 'baz', 'meow');
var_dump($arr); // ['foo' => 'bar', 'baz' => 'meow']