预定义常量
这里列出的所有常量将作为 PHP 核心的一部分一直有效。
-
CASE_LOWER (
integer)
-
CASE_LOWER 用在 array_change_key_case() 中将数组的键名转换成小写字母。这也是 array_change_key_case() 的默认值。
CASE_UPPER (
integer)
-
CASE_UPPER 用在 array_change_key_case() 中将数组的键名转换成大写字母。
排序顺序标识:
-
SORT_ASC (
integer)
-
SORT_ASC 用在 array_multisort() 函数中,使其升序排列。
SORT_DESC (
integer)
-
SORT_DESC 用在 array_multisort() 函数中,使其降序排列。
排序类型标识:用于各种排序函数
参见
参见 is_array(),explode(),implode(),split(),preg_split() 和 unset()。
【译者注】本节中出现的“结合数组”指的是键名为字符串的数组,“索引”和“键名”指的是同一样东西。“索引”多指数组的数字形式的下标。
-
目录
array_change_key_case -- 返回字符串键名全为小写或大写的数组
array_chunk -- 将一个数组分割成多个
array_combine -- 创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值
array_count_values -- 统计数组中所有的值出现的次数
array_diff_assoc -- 带索引检查计算数组的差集
array_diff_key -- Computes the difference of arrays using keys for comparison
array_diff_uassoc -- Computes the difference of arrays with additional index check which is performed by a user supplied callback function
array_diff_ukey -- Computes the difference of arrays using a callback function on the keys for comparison
array_diff -- 计算数组的差集
array_fill -- 用给定的值填充数组
array_filter -- 用回调函数过滤数组中的单元
array_flip -- 交换数组中的键和值
array_intersect_assoc -- 带索引检查计算数组的交集
array_intersect_key -- Computes the intersection of arrays using keys for comparison
array_intersect_uassoc -- Computes the intersection of arrays with additional index check, compares indexes by a callback function
array_intersect_ukey -- Computes the intersection of arrays using a callback function on the keys for comparison
array_intersect -- 计算数组的交集
array_key_exists -- 检查给定的键名或索引是否存在于数组中
array_keys -- 返回数组中所有的键名
array_map -- 将回调函数作用到给定数组的单元上
array_merge_recursive -- 递归地合并两个或多个数组
array_merge -- 合并两个或多个数组
array_multisort -- 对多个数组或多维数组进行排序
array_pad -- 用值将数组填补到指定长度
array_pop -- 将数组最后一个单元弹出(出栈)
array_push -- 将一个或多个单元压入数组的末尾(入栈)
array_rand -- 从数组中随机取出一个或多个单元
array_reduce -- 用回调函数迭代地将数组简化为单一的值
array_reverse -- 返回一个单元顺序相反的数组
array_search -- 在数组中搜索给定的值,如果成功则返回相应的键名
array_shift -- 将数组开头的单元移出数组
array_slice -- 从数组中取出一段
array_splice -- 把数组中的一部分去掉并用其它值取代
array_sum -- 计算数组中所有值的和
array_udiff_assoc -- Computes the difference of arrays with additional index check, compares data by a callback function
array_udiff_uassoc -- Computes the difference of arrays with additional index check, compares data and indexes by a callback function
array_udiff -- Computes the difference of arrays by using a callback function for data comparison
array_uintersect_assoc -- Computes the intersection of arrays with additional index check, compares data by a callback function
array_uintersect_uassoc -- Computes the intersection of arrays with additional index check, compares data and indexes by a callback functions
array_uintersect -- Computes the intersection of arrays, compares data by a callback function
array_unique -- 移除数组中重复的值
array_unshift -- 在数组开头插入一个或多个单元
array_values -- 返回数组中所有的值
array_walk_recursive -- Apply a user function recursively to every member of an array
array_walk -- 对数组中的每个成员应用用户函数
array -- 新建一个数组
arsort -- 对数组进行逆向排序并保持索引关系
asort -- 对数组进行排序并保持索引关系
compact -- 建立一个数组,包括变量名和它们的值
count -- 统计变量中的单元数目
current -- 返回数组中的当前单元
each -- 返回数组中当前的键/值对并将数组指针向前移动一步
end -- 将数组的内部指针指向最后一个单元
extract -- 从数组中将变量导入到当前的符号表
in_array -- 检查数组中是否存在某个值
key -- 从结合数组中取得键名
krsort -- 对数组按照键名逆向排序
ksort -- 对数组按照键名排序
list -- 把数组中的值赋给一些变量
natcasesort -- 用“自然排序”算法对数组进行不区分大小写字母的排序
natsort -- 用“自然排序”算法对数组排序
next -- 将数组中的内部指针向前移动一位
pos -- 得到数组当前的单元
prev -- 将数组的内部指针倒回一位
range -- 建立一个包含指定范围单元的数组
reset -- 将数组的内部指针指向第一个单元
rsort -- 对数组逆向排序
shuffle -- 将数组打乱
sizeof --
count() 的别名
sort -- 对数组排序
uasort -- 使用用户自定义的比较函数对数组中的值进行排序并保持索引关联
uksort -- 使用用户自定义的比较函数对数组中的键名进行排序
usort -- 使用用户自定义的比较函数对数组中的值进行排序
PHP4.0中共有超过30个新的数组相关函数。其中很多通用函数允许你检查给定数组中是否存在特定对象、对数组元素计数、增加或删除元素,或对元素排序。
以下为引用的内容: <? $namesArray = array("Joe", "Jane", "Bob", "Mary", "Paul", "Eddie", "John"); $lookingFor = "Albert"; if (in_array($lookingFor, $namesArray)) { echo "You've found it!"; } else { echo "Not found in this array!"; } ?> |
如果你改变了$lookingFor的值,将其变为“Mary”,你将得到消息“You've found it!”——因为“Mary”是$namesArray的一部分。
如果希望对数组元素计数,你可以使用count()函数:
以下为引用的内容: <? $namesArray = array("Joe", "Jane", "Bob", "Mary", "Paul", "Eddie", "John"); $count = count($namesArray); ?> $count值将为7。 |
你可以对任何数组添加元素,无论是在已存在数组的开始或末尾。你也可以使用函数以创建一个包含两个或多个数组元素的新数组。合并时每个数组将按需要的顺序排列。如果你的数组已经有内部的排序,你需要对新的合并数组重排序。
让我们从对已存在数组的末尾增添元素开始,使用函数array_push():
以下为引用的内容: <? /* 创建原始数组 */ $fruitArray = array("apple", "orange", "banana", "kiwi", "pear"); /* 加入到原始数组中 */ array_push($fruitArray, "grape", "pineapple", "tomato"); /* 通过其键值列出每个元素*/ while (list($key,$value) = each($fruitArray)) { echo "$key : $value<br>"; } ?> |
这将显示:
以下为引用的内容: 0 : apple 1 : orange 2 : banana 3 : kiwi 4 : pear 5 : grape 6 : pineapple 7 : tomato |
当你需要对数组开头添加元素时,代码非常类似。不同处只是函数名:array_unshift() 而不是array_push()。
以下为引用的内容: <? /* 创建原始数组 */ $fruitArray = array("apple", "orange", "banana", "kiwi", "pear"); /* 加入到原始数组中 */ array_unshift($fruitArray, "grape", "pineapple", "tomato"); /* 通过其键值列出每个元素*/ while (list($key,$value) = each($fruitArray)) { echo "$key : $value<br>"; } ?> |
这将显示:
以下为引用的内容: 0 : grape 1 : pineapple 2 : tomato 3 : apple 4 : orange 5 : banana 6 : kiwi 7 : pear |
函数array_merge()合并两个或更多的数组。
以下为引用的内容: <? /* 创建原始数组 */ $fruitArray = array("apple", "orange", "banana", "kiwi", "pear"); <? /* 创建第二个数组 */ $vegArray = array("carrot", "green beans", "asparagus", "artichoke", "corn"); /* 合并为一个数组 */ $goodfoodArray = array_merge($fruitArray, $vegArray); /* 通过其键值列出每个元素*/ while (list($key,$value) = each($goodfoodArray)) { echo "$key : $value<br>"; } ?> |
这将显示:
以下为引用的内容: 0 : apple 1 : orange 2 : banana 3 : kiwi 4 : pear 5 : carrot 6 : green beans 7 : asparagus 8 : artichoke 9 : corn |
现在已经对数组进行了增加元素和合并,现在来练习删除元素函数。你可以使用函数array_pop()从一数组末尾删除一个元素。如果使用函数 array_shift(),则从一数组开头删除一个元素。而实际上当你从数组删除元素时,此元素对你而言仍然可用——当你从已存在的数组中对元素进行 pop 或 shift时。
使用array_pop()函数从数组末尾删除一个值:
以下为引用的内容: <? /* 创建一数组*/ $fruitArray = array("apple", "orange", "banana", "kiwi", "pear"); /* 在末尾弹出某值 */ $popped = array_pop($fruitArray); /* 列出新数组内容,以及弹出的值*/ while (list($key,$value) = each($fruitArray)) { echo "$key : $value<br>"; } echo "<br>and finally, in $popped: $popped"; ?> |
这将显示:
以下为引用的内容: 0 : apple 1 : orange 2 : banana 3 : kiwi and finally, in $popped: pear Next, delete an element from the end of an array: ??????????? |
下面,从数组末尾删除某值:
以下为引用的内容: <? /* 创建一数组*/ $fruitArray = array("apple", "orange", "banana", "kiwi", "pear"); /* 从数组头部移出某值 */ $shifted = array_shift($fruitArray); /* 列出新数组的内容以及移出的值*/ while (list($key,$value) = each($fruitArray)) { echo "$key : $value<br>"; } echo "<br>and finally, in $shifted: $shifted"; ?> |
这将显示:
以下为引用的内容: 0 : orange 1 : banana 2 : kiwi 3 : pear and finally, in $shifted: apple |
有很多函数可以帮助你对数组元素排序。但我将会演示基本的排序以帮助你了解其过程:
以下为引用的内容: <? /* 创建原始数组 */ $fruitArray = array("apple", "orange", "banana", "kiwi", "pear"); /* 排序 */ sort($fruitArray); /* 对其重设以正确从头到尾显示数组 */ /* 通过其键值列出每个元素*/ while (list($key,$value) = each($fruitArray)) { echo "$key : $value<br>"; } ?> |
这将显示:
以下为引用的内容: 0 : apple 1 : banana 2 : kiwi 3 : orange 4 : pear |