Constants This class defines the following constants:

ArrayUtils::JOIN_INNER
ArrayUtils::JOIN_OUTER

Methods This class defines the following methods:

static isCollection (array $array) -> bool

Check whether the given array is a collection of data, ie.

multidimensional array with a list of data rows.

Example:

 echo \MD\Foundation\Utils\ArrayUtils::isCollection(array(
     array('id' => 1),
     array('id' => 2),
     array('id' => 3)
 ));
 // -> true

 echo \MD\Foundation\Utils\ArrayUtils::isCollection(array(
     'id' => 1,
     'title' => 'Lorem ipsum'
 ));
 // -> false
Arguments:
array $array

Array to check.

Returns:
bool

static resetKeys (array $array) -> array

Resets keys of an array to numerical values starting with 0.

Example:

 echo \MD\Foundation\Utils\ArrayUtils::resetKeys(array(
     'a' => 0,
     'b' => 3,
     'c' => '8'
 ));
 // -> array(0, 3, '8')
Arguments:
array $array

Array to reset.

Returns:
array

static pluck (array $array, string $key, bool $preserveIndex = false) -> array

Returns an array with a list of all (non-unique) values assigned to a key in a collection.

Example:

 echo \MD\Foundation\Utils\ArrayUtils::pluck(array(
     array('id' => 4, 'title' => 'Lipsum', 'author' => 'John'),
     array('id' => 3, 'title' => 'Lorem ipsum', 'author' => 'Jane'),
     array('id' => 2, 'title' => 'Dolor sit amet', 'author' => 'Doe')
 ), 'title');
 // -> array('Lipsum', 'Lorem ipsum', 'Dolor sit amet')
Arguments:
array $array

Array collection to get the values from.

string $key

Get values of this key.

bool $preserveIndex

[optional] Should the collection index be preserved or not? Default: false.

Returns:
array

static keyFilter (array $array, string $key, bool $preserveIndex = false) -> array

Deprecated! This method has been deprecated. Do not use it anymore!

Alias for ::pluck().

Arguments:
array $array

Array collection to get the values from.

string $key

Get values of this key.

bool $preserveIndex

[optional] Should the collection index be preserved or not? Default: false.

Returns:
array

static filter (array $array, string $key, mixed $value, bool $preserveIndex = false) -> array

Returns all values from an array collection that match the given value of the given associative level 2 key.

Example:

 echo \MD\Foundation\Utils\ArrayUtils::filter(array(
     array('id' => 3, 'title' => 'Lorem ipsum', 'featured' => true),
     array('id' => 2, 'title' => 'Lipsum', 'featured' => true),
     array('id' => 4, 'title' => 'Dolor sit amet', 'featured' => false),
     array('id' => 6, 'title' => 'Adipiscit elit', 'featured' => true)
 ), 'featured', true);
 // -> array(
 //      array('id' => 3, 'title' => 'Lorem ipsum', 'featured' => true),
 //      array('id' => 2, 'title' => 'Lipsum', 'featured' => true),
 //      array('id' => 6, 'title' => 'Adipiscit elit', 'featured' => true)
 // )
Arguments:
array $array

Array to filter from.

string $key

Key to filter by.

mixed $value

Value to filter by.

bool $preserveIndex

[optional] Should the index of the array collection be preserved or not? Default: false.

Returns:
array

static filterByKeyValue (array $array, string $key, mixed $value, bool $preserveIndex = false) -> array

Deprecated! This method has been deprecated. Do not use it anymore!

Alias for ::filter().

Arguments:
array $array

Array to filter from.

string $key

Key to filter by.

mixed $value

Value to filter by.

bool $preserveIndex

[optional] Should the index of the array collection be preserved or not? Default: false.

Returns:
array

static indexBy (array $array, string $key) -> array

Assigns a value under a key of an array collection to its main (top level) key.

Example:

 echo \MD\Foundation\Utils\ArrayUtils::indexBy(array(
     array('id' => 3, 'title' => 'Lorem ipsum', 'featured' => true),
     array('id' => 2, 'title' => 'Lipsum', 'featured' => true),
     array('id' => 4, 'title' => 'Dolor sit amet', 'featured' => false),
     array('id' => 6, 'title' => 'Adipiscit elit', 'featured' => true)
 ), 'id');
 // -> array(
 //      3 => array('id' => 3, 'title' => 'Lorem ipsum', 'featured' => true),
 //      2 => array('id' => 2, 'title' => 'Lipsum', 'featured' => true),
 //      4 => array('id' => 4, 'title' => 'Dolor sit amet', 'featured' => false),
 //      6 => array('id' => 6, 'title' => 'Adipiscit elit', 'featured' => true)
 // )
Arguments:
array $array

Array to be re-indexed.

string $key

Key on which to index by.

Returns:
array
Throws:
RuntimeException

When at least one row of the $array doesn't have the $key.

RuntimeException

When values of $key are not unique.

static keyExplode (array $array, string $key) -> array

Deprecated! This method has been deprecated. Do not use it anymore!

Alias for ::indexBy().

Arguments:
array $array

Array to be re-indexed.

string $key

Key on which to index by.

Returns:
array

static groupBy (array $array, string $key, bool $preserveIndex = false) -> array

Group all items from the array collection by the value of a key.

Example:

 echo \MD\Foundation\Utils\ArrayUtils::groupBy(array(
     array('id' => 3, 'title' => 'Lorem ipsum', 'categoryId' => 4),
     array('id' => 2, 'title' => 'Lipsum', 'categoryId' => 2),
     array('id' => 4, 'title' => 'Dolor sit amet', 'categoryId' => 3),
     array('id' => 6, 'title' => 'Adipiscit elit', 'categoryId' => 4),
     array('id' => 1, 'title' => 'Aequetam alitat', 'categoryId' => 3)
 ), 'categoryId');
 // -> array(
 //      4 => array(
 //          array('id' => 3, 'title' => 'Lorem ipsum', 'categoryId' => 4),
 //          array('id' => 6, 'title' => 'Adipiscit elit', 'categoryId' => 4)
 //      ),
 //      2 => array(
 //          array('id' => 2, 'title' => 'Lipsum', 'categoryId' => 2)
 //      ),
 //      3 => array(
 //          array('id' => 4, 'title' => 'Dolor sit amet', 'categoryId' => 3),
 //          array('id' => 1, 'title' => 'Aequetam alitat', 'categoryId' => 3)
 //      )
 // )
Arguments:
array $array

Array collection.

string $key

Key to group by.

bool $preserveIndex

[optional] Should the index of the array collection be preserved or not? Default: false.

Returns:
array

static categorizeByKey (array $array, string $key, bool $preserveIndex = false) -> array

Deprecated! This method has been deprecated. Do not use it anymore!

Alias for ::groupBy().

Arguments:
array $array

Array collection.

string $key

Key to group by.

bool $preserveIndex

[optional] Should the index of the array collection be preserved or not? Default: false.

Returns:
array

static implode (array $array, string $separator = ',', string $lastSeparator = null) -> string

Implodes values of an array, but allows alternative separator between last two elements.

Example:

 echo \MD\Foundation\Utils\ArrayUtils::implode(array('a', 'b', 'c', 'd'), ', ', ' and ');
 // -> 'a, b, c and d'

 echo \MD\Foundation\Utils\ArrayUtils::implode(array('a', 'b'), ', ', ' and ');
 // -> 'a and b'
Arguments:
array $array

Array to be imploded.

string $separator

[optional] Separator to implode with. Default: ,.

string $lastSeparator

[optional] Separator to use between last two elements - if null then $separator will be used. Default: null.

Returns:
string

static implodeByKey (array $array, string $key, string $separator = ',') -> string

Implodes specific key values from an array collection.

Example:

 echo \MD\Foundation\Utils\ArrayUtils::implodeByKey(array(
     array('id' => 3, 'title' => 'Lorem ipsum', 'categoryId' => 4),
     array('id' => 2, 'title' => 'Lipsum', 'categoryId' => 2),
     array('id' => 4, 'title' => 'Dolor sit amet', 'categoryId' => 3),
     array('id' => 6, 'title' => 'Adipiscit elit', 'categoryId' => 4),
     array('id' => 1, 'title' => 'Aequetam alitat', 'categoryId' => 3)
 ), 'id');
 // -> '3,2,4,6,1'
Arguments:
array $array

Array collection to implode.

string $key

Implode values assigned to this key.

string $separator

[optional] Separator to implode by. Default: ,.

Returns:
string

static keyPosition (array $array, string $key) -> int|bool

Get the numerical index for an associative key.

In other words: returns numerical position of a key in an associative array.

Example:

 echo \MD\Foundation\Utils\ArrayUtils::keyPosition(array(
     'one' => 'frodo',
     'two' => 'sam',
     'three' => 'pippin',
     'four' => 'merry'
 ), 'three');
 // -> 2

Returns false if the key doesn't exist.

Arguments:
array $array

Associative array to search through.

string $key

Key name.

Returns:
int|bool

static keyRemove (array $array, string $key) -> array

Removes a key from all rows in an array collection.

Example:

 echo \MD\Foundation\Utils\ArrayUtils::keyRemove(array(
     array('id' => 3, 'title' => 'Lorem ipsum', 'categoryId' => 4),
     array('id' => 2, 'title' => 'Lipsum', 'categoryId' => 2),
     array('id' => 4, 'title' => 'Dolor sit amet', 'categoryId' => 3),
     array('id' => 6, 'title' => 'Adipiscit elit', 'categoryId' => 4),
     array('id' => 1, 'title' => 'Aequetam alitat', 'categoryId' => 3)
 ), 'categoryId');
 // -> array('id' => 3, 'title' => 'Lorem ipsum'),
 // -> array('id' => 2, 'title' => 'Lipsum'),
 // -> array('id' => 4, 'title' => 'Dolor sit amet'),
 // -> array('id' => 6, 'title' => 'Adipiscit elit'),
 // -> array('id' => 1, 'title' => 'Aequetam alitat')
Arguments:
array $array

Array collection.

string $key

Key to be removed.

Returns:
array

static keyAdd (array $array, string $key, mixed $value = null) -> array

Adds a key to every row in an array collection.

Optionally you can specify what value to be set under that key.

It will overwrite existing key.

Example:

 echo \MD\Foundation\Utils\ArrayUtils::keyAdd(array(
     array('id' => 3, 'title' => 'Lorem ipsum', 'categoryId' => 4),
     array('id' => 2, 'title' => 'Lipsum', 'categoryId' => 2),
     array('id' => 4, 'title' => 'Dolor sit amet', 'categoryId' => 3),
     array('id' => 6, 'title' => 'Adipiscit elit', 'categoryId' => 4),
     array('id' => 1, 'title' => 'Aequetam alitat', 'categoryId' => 3)
 ), 'featured', true);
 // array('id' => 3, 'title' => 'Lorem ipsum', 'categoryId' => 4, 'featured' => true),
 // array('id' => 2, 'title' => 'Lipsum', 'categoryId' => 2, 'featured' => true),
 // array('id' => 4, 'title' => 'Dolor sit amet', 'categoryId' => 3, 'featured' => true),
 // array('id' => 6, 'title' => 'Adipiscit elit', 'categoryId' => 4, 'featured' => true),
 // array('id' => 1, 'title' => 'Aequetam alitat', 'categoryId' => 3, 'featured' => true)
Arguments:
array $array

Array collection.

string $key

Key to be added.

mixed $value

[optional] Optional value to be set under this key. Default: null.

Returns:
array

static removeValues (array $array, array $values) -> array

Removes all $values from the $array.

It doesn't change the array keys.

Example:

 echo \MD\Foundation\Utils\ArrayUtils::removeValues(
     array('one', 'two', 'three', 'four'),
     array('two', 'three')
 );
 // -> array(0 => 'one', 3 => 'four')
Arguments:
array $array

Array to remove items from.

array $values

Values to be removed.

Returns:
array

static hasValue (array $array, array $values) -> boolean

Checks whether $array has at least one of the $values.

Returns false if $values is empty.

Example:

 echo \MD\Foundation\Utils\ArrayUtils::hasValue(array('one', 'two', 'three'), 'two');
 // -> true
Arguments:
array $array

Array to be checked.

array $values

Values to be found.

Returns:
boolean

static multiSort (array $array, string $key, bool $reverse = false) -> array

Sorts an array collection by value of a row key.

Example:

 echo \MD\Foundation\Utils\ArrayUtils::multiSort(array(
     array('id' => 3, 'title' => 'Lorem ipsum', 'categoryId' => 4),
     array('id' => 2, 'title' => 'Lipsum', 'categoryId' => 2),
     array('id' => 4, 'title' => 'Dolor sit amet', 'categoryId' => 3),
     array('id' => 6, 'title' => 'Adipiscit elit', 'categoryId' => 4),
     array('id' => 1, 'title' => 'Aequetam alitat', 'categoryId' => 3)
 ), 'id');
 // -> array('id' => 1, 'title' => 'Aequetam alitat', 'categoryId' => 3),
 // -> array('id' => 2, 'title' => 'Lipsum', 'categoryId' => 2),
 // -> array('id' => 3, 'title' => 'Lorem ipsum', 'categoryId' => 4),
 // -> array('id' => 4, 'title' => 'Dolor sit amet', 'categoryId' => 3),
 // -> array('id' => 6, 'title' => 'Adipiscit elit', 'categoryId' => 4)
Arguments:
array $array

Array collection to sort.

string $key

Key to sort by.

bool $reverse

[optional] true for descending order, false for ascending. Default: false.

Returns:
array

static sortPaths (array $paths, boolean $rootFirst = false, boolean $separator = DIRECTORY_SEPARATOR) -> array

Sorts an array of paths in either "child first" or "root first" order.

Example:

 echo \MD\Foundation\Utils\ArrayUtils::sortPaths(array(
     'company/wizards/gandalf.txt',
     'company/dwarves/oin.txt',
     'company/dwarves/thorin.txt',
     'company/bilbo.txt',
     'company/dwarves/bifur.txt',
     'company/dwarves/bombur.txt',
     'company/wizards/radagast.txt'
 ));
 // -> array(
 //      'company/dwarves/bifur.txt',
 //      'company/dwarves/bombur.txt',
 //      'company/dwarves/oin.txt',
 //      'company/dwarves/thorin.txt',
 //      'company/wizards/gandalf.txt',
 //      'company/wizards/radagast.txt',
 //      'company/bilbo.txt'
 // );
Arguments:
array $paths

Array of paths (strings).

boolean $rootFirst

[optional] Root first? Default: false (child first).

boolean $separator

[optional] Used path separator. Default: DIRECTORY_SEPARATOR.

Returns:
array

static pushAfter (array $array, mixed $input, int|string $position, string $key = null) -> array

Push a value after another one in an array

Example:

 echo \MD\Foundation\Utils\ArrayUtils::pushAfter(array(
     'one' => 'frodo',
     'two' => 'sam',
     'three' => 'pippin',
     'four' => 'merry'
 ), 'bill', 'two', 'mule');
 // -> array(
 //      'one' => 'frodo',
 //      'two' => 'sam',
 //      'mule' => 'bill',
 //      'three' => 'pippin',
 //      'four' => 'merry'
 // );
Arguments:
array $array

Array to push into.

mixed $input

Value to push.

int|string $position

Position or key after which to push the value.

string $key

[optional] Specific key assigned to the value. Default: null.

Returns:
array

static filterKeys (array $array, array $allowedKeys) -> array

Filters the array so that it contains only the keys from the $allowedKeys list.

Ie. removes all keys that are not in $allowedKeys array.

Example:

 echo \MD\Foundation\Utils\ArrayUtils::filterKeys(
     array('id' => 2, 'title' => 'Lipsum', 'deleted' => false, 'featured' => true, 'slug' => 'lipsum.html'),
     array('title', 'featured')
 );
 // -> array('title' => 'Lipsum', 'featured' => true)

Useful for filtering POST-ed input.

Arguments:
array $array

Array to be filtered.

array $allowedKeys

List of keys that are allowed in the $array.

Returns:
array

static flatten (array $array) -> array

Flattens an array, makes it 1-dimensional.

Example:

 echo \MD\Foundation\Utils\ArrayUtils::flatten(array(
     array('id' => 2, 'title' => 'Lorem ipsum'),
     array('id' => 3, 'title' => 'Lipsum.com'),
     array('id' => 5, 'title' => 'Dolor sit amet')
 ));
 // -> array(2, 'Lorem ipsum', 3, 'Lipsum.com', 5, 'Dolor sit amet');
Arguments:
array $array

Array to be flattened.

Returns:
array

static merge () -> array

Performs a merge between n arrays, where the last array has the highest priority.

You can pass as many arrays as you want.

Example:

 echo \MD\Foundation\Utils\ArrayUtils::merge(
     array('one' => 'frodo', 'two' => 'bilbo'),
     array('two' => 'sam'),
     array('mule' => 'bill'),
     array('three' => 'pippin', 'four' => 'merry')
 );
 // -> array('one' => 'frodo', 'two' => 'sam', 'mule' => 'bill', 'three' => 'pippin', 'four' => 'merry')
Returns:
array

static mergeDeep (array $into, array $from) -> array

Performs a deep merge between two arrays.

Example:

 echo \MD\Foundation\Utils\ArrayUtils::mergeDeep(array(
     'settings' => array(
         'login_enabled' => true,
         'users' => array('Jane', 'John')
     )
 ), array(
     'settings' => array(
         'logout_enabled' => false
         'users' => array('Doe')
     ),
     'config' => array()
 ));
 // -> array(
 //      'settings' => array(
 //          'login_enabled' => true,
 //          'users' => array('Jane', 'John', 'Doe'),
 //          'logout_enabled' => false
 //      ),
 //      'config' => array()
 // )
Arguments:
array $into

Array to merge into.

array $from

Array to merge to.

Returns:
array

static join (array $into, array $from, string $onKey, string $intoKey, string $fromKey, string $type = self::JOIN_OUTER) -> array

Joins items from the $from array collection into the $into collection based on matching criteria.

Default type of join is outer meaning that if matching row wasn't found in the second collection the row in the first collection will not be removed (this can be altered by setting the $type argument to ArrayUtils::JOIN_OUTER).

Example:

 $posts = array(
     array('id' => 3, 'title' => 'Lorem ipsum', 'imageId' => 5),
     array('id' => 4, 'title' => 'Lipsum', 'imageId' => 6),
     array('id' => 5, 'title' => 'Muspi merol', 'imageId' => 5),
     array('id' => 6, 'title' => 'Dolor sit amet', imageId' => 34)
 );
 $images = array(
     array('id' => 5, 'url' => 'http://replygif.net/i/1452.gif'),
     array('id' => 6, 'url' => 'http://replygif.net/i/1448.gif')
 );
 echo \MD\Foundation\Utils\ArrayUtils::join($posts, $images, 'imageId', 'image', 'id');
 // -> array(
 // ->   array('id' => 3, 'title' => 'Lorem ipsum', 'imageId' => 5,
 // ->       'image' => array('id' => 5, 'url' => 'http://replygif.net/i/1452.gif')),
 // ->   array('id' => 4, 'title' => 'Lipsum', 'imageId' => 6,
 // ->       'image' => array('id' => 6, 'url' => 'http://replygif.net/i/1448.gif')),
 // ->   array('id' => 5, 'title' => 'Muspi merol', 'imageId' => 5,
 // ->       'image' => array('id' => 5, 'url' => 'http://replygif.net/i/1452.gif')),
 // ->   array('id' => 6, 'title' => 'Dolor sit amet', imageId' => 34)
 // -> )

In the above example, if we added 6ths argument ArrayUtils::JOIN_INNER then the last $posts element (with id = 6) would not be included in the ::join() results.

Arguments:
array $into

Collection of data in which to put values of $from.

array $from

Collection of data from which get values to put into $into.

string $onKey

What key from $into to compare on?

string $intoKey

Into what key to put values from $from.

string $fromKey

What key from $from to compare on?

string $type

[optional] Type of join. Can also be ArrayUtils::JOIN_INNER which will remove all items from $into that don't have values in $from. Default: ArrayUtils::JOIN_OUTER.

Returns:
array

static checkValues (array $array, array $keys) -> bool

Check whether the specified keys are set inside the given array and are not empty.

Returns true if all is correct, false otherwise.

String values are trimmed before checking, so (single space) is an invalid value.

Example:

 echo \MD\Foundation\Utils\ArrayUtils::checkValues(array(
     'id' => 5,
     'title' => 'Lorem ipsum',
     'slug' => ''
 ), array('id', 'title', 'slug', 'author'));
 // -> false

The above example fails because slug key is empty and there is no author key.

Arguments:
array $array

Array to check.

array $keys

Array of keys to check.

Returns:
bool

static cleanEmpty (array $array) -> array

Removes empty keys from an array.

Example:

 echo \MD\Foundation\Utils\ArrayUtils::cleanEmpty(array(
     'one' => 'frodo',
     'two' => 'sam',
     'three' => '',
     'four' => null,
     'five' => 0,
     'six' => false
 ));
 // -> array('one' => 'frodo', 'two' => 'sam', 'five' => 0, 'six' => false)
Arguments:
array $array

Array to clean.

Returns:
array

static toQueryString (array $array) -> string

Parses an array into a HTTP query string.

The query string will not include the initial ? sign, so it can be appended to an existing query string.

Example:

 echo \MD\Foundation\Utils\ArrayUtils::toQueryString(array(
     'key' => 'val',
     'key1' => array(
         'subkey' => 'subval',
         'subkey1' => 'subval1'
     ),
     'key2' => array('a', 'b', 'c', 'd')
 ));
 // -> key=val&key1%5Bsubkey%5D=subval&key1%5Bsubkey1%5D=subval1&key2%5B0%5D=a&key2%5B1%5D=b&key2%5B2%5D=c&key2%5B3%5D=d
Arguments:
array $array

Array to be converted to query string.

Returns:
string

static toObject (array $array, object $object = null) -> object

Converts the given array to an object.

The conversion is "deep", ie. all dimensions will be converted.

By default it creates an object of \StdClass, but you can pass your own object as the 2nd argument. The array values will be set as public properties on the object.

Arguments:
array $array

Array to convert to object.

object $object

[optional] Object to which assign the properties, usually for internal use. Default: null.

Returns:
object

static fromObject (object|array $object, array $parent = array(), array $keys = array()) -> array

Creates an array from a given object The conversion is "deep", ie.

all dimensions will be converted.

Arguments:
object|array $object

Object to be converted to an array or an array of objects (collection).

array $parent

[optional] Usually for internal use of the function. A reference to parent array. Default: array().

array $keys

[optional] If you don't want the whole object converted to array, specify the names of keys that you are interested in. Default: array().

Returns:
array

static dot (array $array, string $prefix = null) -> array

Flatten a multi-dimensional associative array to dot notation.

Example:

 echo \MD\Foundation\Utils\ArrayUtils::dot(array(
     'flat' => 'bar',
     'foo' => array(
         'bar' => array(
             'baz' => true,
             'bat' => false
         )
     )
 ));
 // -> array('flat' => 'bar', 'foo.bar.baz' => true, 'foo.bar.bat' => false)
Arguments:
array $array

Array to be converted to dot notation.

string $prefix

[optional] Prepend this string to all keys. Default: null.

Returns:
array

static get (array $array, string $key, mixed $default = null) -> mixed

Returns value from the key at given path (using dot notation).

Example:

 echo \MD\Foundation\Utils\ArrayUtils::get(array(
     'flat' => 'bar',
     'foo' => array(
         'bar' => array(
             'baz' => true,
             'bat' => false
         )
     )
 ), 'foo.bar.baz');
 // -> true
Arguments:
array $array

Array to get the value from.

string $key

Key or path to key.

mixed $default

[optional] Default value if the key was not found. Default: null.

Returns:
mixed

static set (array &$array, string $key, mixed $value) -> array

Set an array item to a given value using a dot notation.

If no key is given to the method, the entire array will be replaced.

Returns the array.

Arguments:
array & $array

Array to which the key should be added. Passed by reference.

string $key

Key or path to key.

mixed $value

Value to be set at the given path.

Returns:
array