选择语言 :

Core_Arr
    └ ArrayIterator PHP

数组核心类

常量
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.
author
呼吸二氧化碳 jonwang@myqee.com
category
MyQEE
package
System
subpackage
Core
copyright
Copyright © 2008-2013 myqee.com
license
http://www.myqee.com/license.html

$this->is_empty( )


$this->as_array( $key = null, $value = null)

获取数组

参数列表

参数 类型 描述 默认值
$key string Column for associative keys null
$value string Column for values null
返回值
  • array

Arr::is_assoc( $array )

判断一个数组是否递增key的数组

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

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

参数列表

参数 类型 描述 默认值
$array array Array to check
返回值
  • boolean

Arr::is_array( $value )

判断是否数组或对象型数组

// 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

Arr::path( $array , $path , $default = null, $delimiter = null)

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

Arr::set_path( & $array , $path , $value , $delimiter = null)

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

Arr::range( $step = 10, $max = 100)

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

Arr::get( $array , $key , $default = null)

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

Arr::extract( $array , $keys , $default = null)

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

Arr::pluck( $array , $key )

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

// Get all of the "id" values from a result
$ids = Arr::pluck($result, 'id');
A list of arrays is an array that contains arrays, eg: array(array $a, array $b, array $c, ...)

参数列表

参数 类型 描述 默认值
$array array List of arrays to check
$key string Key to pluck
返回值
  • array

Arr::unshift( & $array , $key , $val )

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

Arr::map( $callback , $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);
Unlike array_map, this method requires a callback and will only map

a single array.

参数列表

参数 类型 描述 默认值
$callback mixed Callback applied to every element in the array
$array array Array to map
返回值
  • array

Arr::merge( $a1 , $a2 )

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

Arr::overwrite( $array1 , $array2 )

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

Arr::callback( $str )

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, params

Arr::flatten( $array )

Convert 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');
The keys of array values will be discarded.

参数列表

参数 类型 描述 默认值
$array array Array to flatten
返回值
  • array