Skip to content

Latest commit

 

History

History
50 lines (39 loc) · 1.11 KB

README.md

File metadata and controls

50 lines (39 loc) · 1.11 KB

PHP KeyGet

Build Status Packagist

Library solves routine actions like:

  • Get array value by key/index if exists or return default value
  • Set array key/index if not exists

Installation

System requirements:

PHP >= 5.0 (tests for 7.1 up to nightly)

Composer:
composer require io-developer/php-keyget

or composer.json:

"require": {
    "io-developer/php-keyget": "*"
}

Examples

Get value by key of default if not exists
<?php

$arr = ['foo' => 'bar'];
var_dump([
    key_get($arr, 'foo'), // 'bar'
    key_get($arr, 'baz'), // null
    key_get($arr, 'baz', 'Ooops!'), // 'Ooops!'
]);
Set value if key not exists
<?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']