数组核心类
常量 | |
---|---|
Arr::STD_PROP_LIST |
integer 1 |
Arr::ARRAY_AS_PROPS |
integer 2 |
API - Core_Arr
- Arr::$delimiter
- Arr::is_empty
- Arr::as_array - 获取数组
- Arr::is_assoc - 判断一个数组是否递增key的数组
- Arr::is_array - 判断是否数组或对象型数组
- Arr::path - Gets a value from an array using a dot separated path.
- Arr::set_path - Set a value on an array by path.
- Arr::range - Fill an array with a range of numbers.
- Arr::get - Retrieve a single key from an array. If the key does not exist in the
- Arr::extract - Retrieves multiple keys from an array. If the key does not exist in the
- Arr::pluck - Retrieves muliple single-key values from a list of arrays.
- Arr::unshift - Adds a value to the beginning of an associative array.
- Arr::map - Recursive version of [array_map](http://php.net/array_map), applies the
- Arr::merge - Merges one or more arrays recursively and preserves all keys.
- Arr::overwrite - Overwrites an array with values from input arrays.
- Arr::callback - Creates a callable function and parameter list from a string representation.
- Arr::flatten - Convert a multi-dimensional array into a single-dimensional array.
继承自父类的方法和变量
- PHPArrayIterator::__construct
- PHPArrayIterator::offsetExists
- PHPArrayIterator::offsetGet
- PHPArrayIterator::offsetSet
- PHPArrayIterator::offsetUnset
- PHPArrayIterator::append
- PHPArrayIterator::getArrayCopy
- PHPArrayIterator::count
- PHPArrayIterator::getFlags
- PHPArrayIterator::setFlags
- PHPArrayIterator::asort
- PHPArrayIterator::ksort
- PHPArrayIterator::uasort
- PHPArrayIterator::uksort
- PHPArrayIterator::natsort
- PHPArrayIterator::natcasesort
- PHPArrayIterator::unserialize
- PHPArrayIterator::serialize
- PHPArrayIterator::rewind
- PHPArrayIterator::current
- PHPArrayIterator::key
- PHPArrayIterator::next
- PHPArrayIterator::valid
- PHPArrayIterator::seek
获取数组
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$key |
string |
Column for associative keys | null |
$value |
string |
Column for values | null |
array
判断一个数组是否递增key的数组
// Returns TRUE
Arr::is_assoc(array('username' => 'john.doe'));
// Returns FALSE
Arr::is_assoc('foo', 'bar');
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$array |
array |
Array to check |
boolean
判断是否数组或对象型数组
// 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());
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$value |
mixed |
Value to check |
boolean
Gets a value from an array using a dot separated path.
// 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'));
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$array |
array |
Array to search | |
$path |
mixed |
Key path string (delimiter separated) or array of keys | |
$default |
mixed |
Default value if the path is not set | null |
$delimiter |
string |
Key path delimiter | null |
mixed
Set a value on an array by path.
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$array |
array |
Array to update | |
$path |
string |
Path | |
$value |
mixed |
Value to set | |
$delimiter |
string |
Path delimiter | null |
Fill an array with a range of numbers.
// Fill an array with values 5, 10, 15, 20
$values = Arr::range(5, 20);
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$step |
integer |
Stepping | integer 10 |
$max |
integer |
Ending number | integer 100 |
array
Retrieve a single key from an array. If the key does not exist in the array, the default value will be returned instead.
// Get the value "username" from $_POST, if it exists
$username = Arr::get($_POST, 'username');
// Get the value "sorting" from $_GET, if it exists
$sorting = Arr::get($_GET, 'sorting');
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$array |
array |
Array to extract from | |
$key |
string |
Key name | |
$default |
mixed |
Default value | null |
mixed
Retrieves multiple keys from an array. If the key does not exist in the array, the default value will be added instead.
// Get the values "username", "password" from $_POST
$auth = Arr::extract($_POST, array('username', 'password'));
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$array |
array |
Array to extract keys from | |
$keys |
array |
List of key names | |
$default |
mixed |
Default value | null |
array
Retrieves muliple single-key values from a list of arrays.
// Get all of the "id" values from a result
$ids = Arr::pluck($result, 'id');
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$array |
array |
List of arrays to check | |
$key |
string |
Key to pluck |
array
Adds a value to the beginning of an associative array.
// Add an empty value to the start of a select list
Arr::unshift($array, 'none', 'Select a value');
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$array |
array |
Array to modify | |
$key |
string |
Array key name | |
$val |
mixed |
Array value |
array
Recursive version of array_map, applies the same callback to all elements in an array, including sub-arrays.
// Apply "strip_tags" to every element in the array
$array = Arr::map('strip_tags', $array);
array_map
, this method requires a callback and will only mapa single array.
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$callback |
mixed |
Callback applied to every element in the array | |
$array |
array |
Array to map |
array
Merges one or more arrays recursively and preserves all keys. Note that this does not work the same as array_merge_recursive!
$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'))
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$a1 |
array |
Initial array | |
$a2 |
array |
Array to merge |
array
Overwrites an array with values from input arrays. Keys that do not exist in the first array will not be added!
$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')
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$array1 |
array |
Master array | |
$array2 |
array |
Input arrays that will overwrite existing values |
array
Creates a callable function and parameter list from a string representation. Note that this function does not validate the callback string.
// 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);
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$str |
string |
Callback string |
array
function, paramsConvert a multi-dimensional array into a single-dimensional array.
$array = array('set' => array('one' => 'something'), 'two' => 'other');
// Flatten the array
$array = Arr::flatten($array);
// The array will now be
array('one' => 'something', 'two' => 'other');
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$array |
array |
Array to flatten |
array