PHP Class Yiinitializr\Helpers\ArrayX

Since: 1.0
Author: Antonio Ramirez ([email protected])
Show file Open project: 2amigos/yiinitializr Class Usage Examples

Public Methods

Method Description
deepMap ( array $array, $callback, boolean $on_nonscalar = FALSE ) : array Returns an array containing all the elements of arr1 after applying the callback function to each one.
deepSearch ( array $array, string $search, boolean $field = FALSE ) : boolean | mixed | string Searches for a given value in an array of arrays, objects and scalar values. You can optionally specify a field of the nested arrays and objects to search in.
divide ( array $array ) : array Divide an array into two arrays. One with keys and the other with values.
except ( array $array, array $keys ) : array Get all of the given array except for a specified array of items.
first ( array $array, Closure $callback, mixed $default = null ) : mixed Return the first element in an array which passes a given truth test.
forget ( array &$array, string $key ) : void Remove an array item from a given array using "dot" notation.
get ( array | CAttributeCollection $array, string $key, mixed $default = null ) : mixed Get an item from an array using "dot" notation.
head ( array $array ) : mixed Return the first element of an array.
merge ( array $a, array $b ) : array Merges two or more arrays into one recursively.
only ( array $array, array $keys ) : array Get a subset of the items from the given array.
pluck ( array $array, string $key ) : array Pluck an array of values from an array.
pop ( &$array, $key, null $default = null ) : mixed | null Removes an item from the given options and returns the value.
set ( array &$array, string $key, mixed $value ) : void Set an array item to a given value using "dot" notation.
stripSlashes ( array $array ) : array Recursively remove slashes from array keys and values.
value ( mixed $value ) : mixed Return the value of the given item.

Method Details

deepMap() public static method

Credits to Util.php
public static deepMap ( array $array, $callback, boolean $on_nonscalar = FALSE ) : array
$array array an array to run through the callback function
$callback Callback function to run for each element in each array
$on_nonscalar boolean whether or not to call the callback function on nonscalar values (objects, resr, etc)
return array

deepSearch() public static method

Credits to Util.php
public static deepSearch ( array $array, string $search, boolean $field = FALSE ) : boolean | mixed | string
$array array The array to search
$search string The value to search for
$field boolean The field to search in, if not specified all fields will be searched
return boolean | mixed | string False on failure or the array key on

divide() public static method

Divide an array into two arrays. One with keys and the other with values.
public static divide ( array $array ) : array
$array array
return array

except() public static method

Get all of the given array except for a specified array of items.
public static except ( array $array, array $keys ) : array
$array array
$keys array
return array

first() public static method

Return the first array element that equals "Taylor" $value = ArrayX::first($array, function($k, $v) {return $v == 'Taylor';}); Return a default value if no matching element is found $value = ArrayX::first($array, function($k, $v) {return $v == 'Taylor'}, 'Default');
public static first ( array $array, Closure $callback, mixed $default = null ) : mixed
$array array
$callback Closure
$default mixed
return mixed

forget() public static method

Remove the $array['user']['name'] item from the array ArrayX::forget($array, 'user.name'); Remove the $array['user']['name']['first'] item from the array ArrayX::forget($array, 'user.name.first');
public static forget ( array &$array, string $key ) : void
$array array
$key string
return void

get() public static method

Get the $array['user']['name'] value from the array $name = ArrayX::get($array, 'user.name'); Return a default from if the specified item doesn't exist $name = ArrayX::get($array, 'user.name', 'Taylor');
public static get ( array | CAttributeCollection $array, string $key, mixed $default = null ) : mixed
$array array | CAttributeCollection
$key string
$default mixed
return mixed

head() public static method

This is simply a convenient wrapper around the "reset" method.
public static head ( array $array ) : mixed
$array array
return mixed

merge() public static method

If each array has an element with the same string key value, the latter will overwrite the former (different from array_merge_recursive). Recursive merging will be conducted if both arrays have an element of array type and are having the same key. For integer-keyed elements, the elements from the latter array will be appended to the former array.
public static merge ( array $a, array $b ) : array
$a array array to be merged to
$b array array to be merged from. You can specifiy additional arrays via third argument, fourth argument etc.
return array the merged array (the original arrays are not changed.)

only() public static method

Get a subset of the items from the given array.
public static only ( array $array, array $keys ) : array
$array array
$keys array
return array

pluck() public static method

Pluck an array of values from an array.
public static pluck ( array $array, string $key ) : array
$array array
$key string
return array

pop() public static method

If no key is found, then default value will be returned.
public static pop ( &$array, $key, null $default = null ) : mixed | null
$array
$key
$default null
return mixed | null

set() public static method

If no key is given to the method, the entire array will be replaced. Set the $array['user']['name'] value on the array ArrayX::set($array, 'user.name', 'Taylor'); Set the $array['user']['name']['first'] value on the array ArrayX::set($array, 'user.name.first', 'Michael');
public static set ( array &$array, string $key, mixed $value ) : void
$array array
$key string
$value mixed
return void

stripSlashes() public static method

Recursively remove slashes from array keys and values.
public static stripSlashes ( array $array ) : array
$array array
return array

value() public static method

If the given item is a Closure the result of the Closure will be returned.
public static value ( mixed $value ) : mixed
$value mixed
return mixed