Skip to content

Solves routine array actions: get value by key if exists or default; set key if not exists. PHP 5, 7

License

Notifications You must be signed in to change notification settings

io-developer/php-keyget

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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']

About

Solves routine array actions: get value by key if exists or default; set key if not exists. PHP 5, 7

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages