Arr helper

Arr::is_assoc

Tests if an array is associative or not.

Parameters

  • $array (array): Array to check

Returns

  • boolean

Example

    // Returns TRUE
    Arr::is_assoc(array('username' => 'john.doe'));

    // Returns FALSE
    Arr::is_assoc('foo', 'bar');

Arr::is_array

Test if a value is an array with an additional check for array-like objects.

Parameters

  • $value (mixed): Value to check

Returns

  • boolean

Example

    // Returns TRUE
    Arr::is_array(array());
    Arr::is_array(new ArrayObject);

    // Returns FALSE
    Arr::is_array(FALSE);
    Arr::is_array('not an array!');
    Arr::is_array(Database::instance());

Arr::path

Gets a value from an array using a dot separated path.

Parameters

  • $array (array): Array to search
  • $path (mixed): Key path string or array of keys (delimited separated)
  • $default (mixed): Default value if the path is not set (Default: null)
  • $delimiter (string): Key path delimiter (Default: null)

Returns

  • mixed

Example

    // Get the value of $array['foo']['bar']
    $value = Arr::path($array, 'foo.bar');

    // Using a wildcard "*" will search intermediate arrays and return an array.

    // Get the values of "color" in theme
    $colors = Arr::path($array, 'theme.*.color');

    // Using an array of keys
    $colors = Arr::path($array, array('theme', '*', 'color'));

Arr::set_path

Set a value on an array by path.

Parameters

  • $array (array): Array to update
  • $path (string): Path
  • $value (mixed): Value to set
  • $delimiter (string): Path delimiter

Returns

  • void

Example

    $data = ['level1' => 'level1a' => 'bar'];

    // Add level2a = foo into level1
    Arr::set_path($data, 'level1.level2a', 'foo');

    // Overwrite level1a
    Add::set_path($data, 'level1.level1a', 'foo');

Arr::range

Fill an array with a range of numbers.

Parameters

  • $step (integer): Stepping
  • $mas (integer): Ending number

Returns

  • array

Example

    // Fill an array with values 5, 10, 15, 20
    $values = Arr::range(5, 20);

Arr::get

Retrieve a single key from an array. If the key does not exist in the array, the default value will be returned instead.

Parameters

  • $array (array): Array to extract from
  • $key (string): Key name
  • $default (mixed): Default value

Returns

  • mixed

Example

    // Get the value "username" from $GLOBALS, if it exists
    $username = Arr::get($GLOBALS, 'username');

    // Get the value "sorting" from $GLOBALS, if it exists
    $sorting = Arr::get($GLOBALS, 'sorting');

Arr::extract

Retrieves multiple paths from an array. If the path does not exist in the array, the default value will be added instead.

Parameters

  • $array (array): Array to extract paths from
  • $array (array): List of path
  • $mixed (mixed): Default value

Returns

  • array

Example

    // Get the values from command line arguments (-a "foo" -b "bar" -c "mamuph")
    // Only "a" and "c" are retrieved.
    $auth = Arr::extract(getops('a:b:c'), array('a', 'c'));

Example

    // Get the value "level1.level2a" from $data (Full array path included)
    $data = array('level1' => array('level2a' => 'value 1', 'level2b' => 'value 2'));
    Arr::extract($data, array('level1.level2a', 'foo'));

Arr::pluck

Retrieves muliple single-key values from a list of arrays.

Parameters

  • $array (array): List of arrays to check
  • $key (string): Key to pluck

Returns

  • array

Example

    // Get all of the "id" values from a result
    $ids = Arr::pluck($result, 'id');

Arr::flatten

Convert a multi-dimensional array into a single-dimensional array.

Parameters

  • $array (array): Array to flatten

Returns

  • array

Example

    $array = array('set' => array('one' => 'something'), 'two' => 'other');

    // The array will now be array('one' => 'something', 'two' => 'other');
    $array = Arr::flatten($array);

Arr::map

Recursive version of array_map, applies one or more callbacks to all elements in an array, including sub-arrays.

Parameters

  • $callbacks (mixed): Array of callbacks to apply to every element in the array
  • $array (array): Array to map
  • $keys (array): Array of keys to apply to

Returns

  • array

Example

    // Apply "strip_tags" to every element in the array
    $array = Arr::map('strip_tags', $array);

    // Apply $this->filter to every element in the array
    $array = Arr::map(array($this,'filter'), $array);

    // Apply strip_tags and $this->filter to every element
    $array = Arr::map(array('strip_tags',array($this,'filter')), $array);

Arr::merge

Recursively merge two or more arrays. Values in an associative array overwrite previous values with the same key. Values in an indexed array are appended, but only when they do not already exist in the result.

Note that this does not work the same as array_merge_recursive!

Parameters

  • $array1 (array): Initial array
  • $array2 (array): Array to merge

Returns

  • array

Example

    $john = array('name' => 'john', 'children' => array('fred', 'paul', 'sally', 'jane'));
    $mary = array('name' => 'mary', 'children' => array('jane'));

    // John and Mary are married, merge them together
    $john = Arr::merge($john, $mary);

    // The output of $john will now be:
    array('name' => 'mary', 'children' => array('fred', 'paul', 'sally', 'jane'))

Arr::overwrite

Overwrites an array with values from input arrays. Keys that do not exist in the first array will not be added!

Parameters

  • $array1 (array): Master array
  • $array2 (array): Input arrays that will overwrite existing values

Returns

  • array

Example

    $a1 = array('name' => 'john', 'mood' => 'happy', 'food' => 'bacon');
    $a2 = array('name' => 'jack', 'food' => 'tacos', 'drink' => 'beer');

    // Overwrite the values of $a1 with $a2
    $array = Arr::overwrite($a1, $a2);

    // The output of $array will now be:
    array('name' => 'jack', 'mood' => 'happy', 'food' => 'tacos')

Arr::callback

Creates a callable function and parameter list from a string representation. Note that this function does not validate the callback string.

Parameters

  • $str (string): Callback string

Returns

  • array
  • function, params

Example

    // Get the callback function and parameters 
    list($func, $params) = Arr::callback('Foo::bar(apple,orange)');

    // Get the result of the callback
    $result = call_user_func_array($func, $params);

Example

    // Get the callback function and parameters
    list($func, $params) = Arr::callback('Foo::bar(1,2,3,hey)');

    // $Func = [0 => "Foo", 1 => "bar"]
    var_dump($func);

    // $params = [0 => "1", 1 => "2", 2 => "3", 3 => "hey"]
    var_dump($params);

Parameters are always passed as string type.
Space separator between the parameters are interpreted as empty spaces.