Home php教程 php手册 数组处理函数库第1/2页

数组处理函数库第1/2页

Jun 13, 2016 pm 12:36 PM
array function deal with Library Establish array user able

array : 建立一个新的数组。
array_walk : 让用户自订函数能处理数组中的每一个元素。
arsort : 将数组的值由大到小排序。
asort : 将数组的值由小到大排序。
count : 计算变量或数组中的元素个数。
current : 返回数组中目前的元素。
each : 返回数组中下一个元素的索引及值。
end : 将数组的内部指针指到最后的元素。
key : 取得数组中的索引资料。
ksort : 将数组的元素依索引排序。
list : 列出数组中元素的值。
next : 将数组的内部指针向后移动。
pos : 返回数组目前的元素。
prev : 将数组的内部指针往前移动。
range : 建立一个整数范围的数组。
reset : 将数组的指针指到数组第一个元素。
rsort : 将数组的值由大到小排序。
shuffle : 将数组的顺序弄混。
sizeof : 获知数组的大小。
sort : 将数组排序。
uasort : 将数组依用户自定的函数排序。
uksort : 将数组的索引依用户自定的函数排序。
usort : 将数组的值依用户自定的函数排序。

array

建立一个新的数组。

语法
array array(...);

返回值:
数组

函数种类
: 资料处理

内容说明: 返回的参数是数组类型。参数可以是带有 => 运算子的索引。array() 其实不是一个正规的函数,它主要是要用来表示数组。

使用范例 :
下面范例用显示如何建立一个二维数组,如何指定联合数组的键值,及如何略过和继续数组中的数字索引。

$fruits = array(
"fruits" => array("a"=>"orange","b"=>"banana","c"=>"apple"),
"numbers" => array(1, 2, 3, 4, 5, 6),
"holes" => array("first", 5 => "second", "third")
);

参考 list()

array_walk
让使用者自订函数能处理数组中的每一个元素。

语法: int array_walk(array arr, string func);

返回值: 整数

函数种类: 资料处理

内容说明 此函数使每个数组元素 arr 依序与函数名称 func 相对应。元素传送到函数 func 的第一个参数,若参数超过一个,则每次都会有警告信息。要处理警告信息,可在本函数前面加上 '@' 字符 (变成 @array_walk);或是使用 error_reporting 函数。

注意: 使用者自订函数 func 真的会将数组元素 arr 依序代入,所以任何对元素所做的改变都会影响到数组本身。

使用范例


$fruits = array("d"=>"lemon","a"=>"orange","b"=>"banana","c"=>"apple");
function test_alter( $item1 ) {
$item1 = 'bogus';
}function test_print( $item2 ) {
echo "$item2
n";
}array_walk( $fruits, 'test_print' );
array_walk( $fruits, 'test_alter' );
array_walk( $fruits, 'test_print' );
?>
参考 each() list()

arsort
将数组的值由大到小排序。
语法: void arsort(array array);
返回值: 无
函数种类:资料处理内容说明这个函数将数组的值重新排序,由大至小排列。数组的索引亦跟着值的顺序而变动。当您在程序中需要重新整理数组值的顺序时,就可以使用这个函数。

使用范例
底下的范例返回的结果为
fruits[a] = orange
fruits[d] = lemon
fruits[b] = banana
fruits[c] = apple。
我们可以看到水果名 (数组值) 已按英文字母的顺序由 z 往 a 重新排序,而索引亦跟着值变动。

$fruits = array("d"=>"lemon","a"=>"orange","b"=>"banana","c"=>"apple");
arsort($fruits);
for(reset($fruits); $key = key($fruits); next($fruits)) {
echo "fruits[$key] = ".$fruits[$key]."n";
}
?>

参考 asort() rsort() ksort() sort()

asort
将数组的值由小到大排序。
语法: void asort(array array);
返回值: 无
函数种类: 资料处理

内容说明 这个函数将数组的值重新排序,由小至大排列。数组的索引亦跟着值的顺序而变动。当您在程序中需要重新整理数组值的顺序时,就可以使用这个函数。

使用范例
底下的范例返回的结果为
fruits[c] = apple
fruits[b] = banana
fruits[d] = lemon
fruits[a] = orange
我们可以看到水果名 (数组值) 已按英
文字母的顺序由 a 往 z 排序,而索引亦跟着值变动。

$fruits = array("d"=>"lemon","a"=>"orange","b"=>"banana","c"=>"apple");
asort($fruits);
for(reset($fruits); $key = key($fruits); next($fruits)) {
echo "fruits[$key] = ".$fruits[$key]."n";
}
?>

参考 arsort() rsort() ksort() sort()

count
计算变量或数组中的元素个数。
语法: int count(mixed var);
返回值: 整数
函数种类: 资料处理

内容说明 这个函数用来计算数组的元素个数 (亦可将变量代入,只不过返回的整数将是 1)。变量还没有配置时,返回值为 0。变量若不是数组,返回值为 1。

参考 sizeof() isset() is_array()

current
返回数组中目前的元素。
语法: mixed current(array array);
返回值: 混合类型资料
函数种类: 资料处理

内容说明 说明: 每一个数组变量都有一个内部指针,指到它的每一个元素。此外,为了交互参考,数组有份所有元素的双向链结表。数组的内部指针指到原先插入的元素上,直到程序执行到有改动数组指针的函数。函数 current() 简单地返回数组元素中目前指到的数组内部指针。它不会改变指针的值,若数组指针指到内部指针表外,则返回 false 的值。

注意: 若数组中包含空的元素 (0 或者 "" 空字符串),则本函数会返回 false 值。要是目前元素是个零值的空元素或者是超出数组指针,结果当然是未定的 false 值。遇到这种情形,可以使用 each() 函数会更适合。

参考 end() next() prev() reset()

each
返回数组中下一个元素的索引及值。
语法: array each(array array);
返回值: 数组
函数种类: 资料处理

内容说明 返回数组为目前数组指针的 索引/值 对。返回的数组有四个元素,依序为 0, 1, 索引, 及值。前述的 0 与 索引 为数组的索引,1 与 值则为数组元素的值。
使用范例
范例一:

$foo = array( "bob", "fred", "jussi", "jouni" );
$bar = each( $foo );
?>
上面的例子,返回数组 $bar 的 索引/值 为
0 => 0
1 => 'bob'
key => 0
value => 'bob'

范例二:

$foo = array( "Robert" => "Bob", "Seppo" => "Sepi" );
$bar = each( $foo );
?>
这个的例子,返回数组 $bar 的 索引/值 为
0 => 'Robert'
1 => 'Bob'
key => 'Robert'
value => 'Bob'

范例三:
each() 函数最典型的例子是拿来与 list() 函数合用,如下例的 $HTTP_POST_VARS 变量。

echo "POST 所送出的值为:
";
while ( list( $key, $val ) = each( $HTTP_POST_VARS ) ) {
echo "$key => $val
";
}
?>

参考 current() key() list() next() prev() reset()

end
将数组的内部指针指到最后的元素。
语法: end(array array);
返回值: 无
函数种类: 资料处理

内容说明 本函数会改变数组的内部指针,它将指针指到最后一个元素上。

参考 current() each() next() reset()

key
取得数组中的索引资料。
语法: mixed key(array array);
返回值: 混合类型资料
函数种类: 资料处理
内容说明
本函数从目前数组的指针,返回其索引

参考 current() next()

ksort
将数组的元素依索引排序。
语法: void ksort(array array);
返回值: 无
函数种类: 资料处理
内容说明
本函数将数组中的元素依索引排序,排序后的索引和值仍然对应

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy May 01, 2024 pm 12:30 PM

Methods for deep copying arrays in PHP include: JSON encoding and decoding using json_decode and json_encode. Use array_map and clone to make deep copies of keys and values. Use serialize and unserialize for serialization and deserialization.

PHP array key value flipping: Comparative performance analysis of different methods PHP array key value flipping: Comparative performance analysis of different methods May 03, 2024 pm 09:03 PM

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values ​​takes a relatively long time.

Complete collection of excel function formulas Complete collection of excel function formulas May 07, 2024 pm 12:04 PM

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods Apr 30, 2024 pm 03:42 PM

The best practice for performing an array deep copy in PHP is to use json_decode(json_encode($arr)) to convert the array to a JSON string and then convert it back to an array. Use unserialize(serialize($arr)) to serialize the array to a string and then deserialize it to a new array. Use the RecursiveIteratorIterator to recursively traverse multidimensional arrays.

Application of PHP array grouping function in data sorting Application of PHP array grouping function in data sorting May 04, 2024 pm 01:03 PM

PHP's array_group_by function can group elements in an array based on keys or closure functions, returning an associative array where the key is the group name and the value is an array of elements belonging to the group.

C++ Function Exception Advanced: Customized Error Handling C++ Function Exception Advanced: Customized Error Handling May 01, 2024 pm 06:39 PM

Exception handling in C++ can be enhanced through custom exception classes that provide specific error messages, contextual information, and perform custom actions based on the error type. Define an exception class inherited from std::exception to provide specific error information. Use the throw keyword to throw a custom exception. Use dynamic_cast in a try-catch block to convert the caught exception to a custom exception type. In the actual case, the open_file function throws a FileNotFoundException exception. Catching and handling the exception can provide a more specific error message.

The role of PHP array grouping function in finding duplicate elements The role of PHP array grouping function in finding duplicate elements May 05, 2024 am 09:21 AM

PHP's array_group() function can be used to group an array by a specified key to find duplicate elements. This function works through the following steps: Use key_callback to specify the grouping key. Optionally use value_callback to determine grouping values. Count grouped elements and identify duplicates. Therefore, the array_group() function is very useful for finding and processing duplicate elements.

Can arrays be used as function parameters? Can arrays be used as function parameters? Jun 04, 2024 pm 04:30 PM

Yes, in many programming languages, arrays can be used as function parameters, and the function will perform operations on the data stored in it. For example, the printArray function in C++ can print the elements in an array, while the printArray function in Python can traverse the array and print its elements. Modifications made to the array by these functions are also reflected in the original array in the calling function.

See all articles