timdev/php-array-utils

Handy php array utilities.

0.1.1 2016-02-04 02:54 UTC

This package is auto-updated.

Last update: 2024-04-20 04:18:25 UTC


README

Everybody who writes a bunch of PHP eventually writes functions to do some common array manipulation tasks.

This is where I'm going to stick all mine. You're welcome to use them, too.

The utilities are implemented as static methods on a class.

As of today, there's only one function:

ArrayUtils::val(array $array, $keys, $default = null)

Digs a value out of an array, or else returns a default value.

use TimDev\ArrayUtils as A;

$array = ['foo' => ['bar' => 'baz']];

// $baz == 'baz'
$baz = A::val($array, ['foo', 'bar']);

// $nope == null
$nope = A::val($array, ['foo', 'bork']);

// $nope == 'squee'
$nope = A::val($array, ['i-do-not-exist'], 'squee');

// if you're just dereferencing by one level, you can pass int/string as second arg:
// $foo == ['bar' => 'baz']
$foo = A::val($array, 'foo');