search
HomeBackend DevelopmentPHP TutorialArray processing function library page 1/2_PHP tutorial
Array processing function library page 1/2_PHP tutorialJul 21, 2016 pm 04:00 PM
arrayfunctiondeal withLibraryEstablisharrayuserable

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";
}
?>
Array processing function library page 1/2_PHP tutorial
参考 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";
}
?>
Array processing function library page 1/2_PHP tutorial
参考 arsort() rsort() ksort() sort()

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

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

参考 sizeof() isset() is_array()

current
Returns the current element in the array.
Syntax: mixed current(array array);
Return value: Mixed type data
Function type: Data processing

Content description: Each array variable has an internal pointer, which refers to to every element of it. Additionally, arrays have a doubly linked list of all elements for cross-reference purposes. The internal pointer of the array points to the originally inserted element until the program executes a function that changes the array pointer. The function current() simply returns the internal array pointer currently pointed to by the array element. It does not change the value of the pointer. If the array pointer points outside the internal pointer table, it returns a value of false.

Note: If the array contains empty elements (0 or "" empty string), this function will return a false value. If the current element is a null element with zero value or exceeds the array pointer, the result is of course an undetermined false value. In this case, it would be more appropriate to use the each() function.

Reference end() next() prev() reset()

each
returns the index and value of the next element in the array.
Syntax: array each(array array);
Return value: array
Function type: Data processing

Content description The returned array is the index/value pair of the current array pointer. The returned array has four elements, in order 0, 1, index, and value. The aforementioned 0 and index are the indexes of the array, and the values ​​1 and 1 are the values ​​of the array elements.
Usage Example
Example 1:

$foo = array( "bob", "fred", "jussi", " jouni" );
$bar = each( $foo );
?>
In the above example, the index/value of the returned array $bar is
0 => 0
1 => 'bob'
key => 0
value => 'bob'

Example 2:
$foo = array( "Robert" => "Bob", "Seppo" => "Sepi" );
$bar = each( $foo );
?>
In this example, the index/value of the returned array $bar is
0 => 'Robert'
1 => 'Bob'
key => 'Robert'
value => ; 'Bob'

Example 3:
The most typical example of each() function is to use it with the list() function, as shown in the following example $ HTTP_POST_VARS variables.

echo "The value sent by POST is:
";
while ( list( $key, $val ) = each( $HTTP_POST_VARS ) ) {
echo " $key => $val
";
}
?>

Reference current() key() list() next() prev() reset()

end
Points the internal pointer of the array to the last element.
Syntax: end(array array);
Return value: None
Function type: Data processing

Content description: This function will change the internal pointer of the array, and it will point the pointer to the last one on the elements.

Reference current() each() next() reset()

key
Get the index data in the array.
Syntax: mixed key(array array);
Return value: Mixed type data
Function type: Data processing
Content description
This function returns the index of the current array from the pointer

Reference current() next()

ksort
Sorts the elements of the array by index.
Syntax: void ksort(array array);
Return value: None
Function type: Data processing
Content description
This function sorts the elements in the array according to the index. The sorted index The sum value still corresponds to

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/317161.htmlTechArticlearray: Create a new array. array_walk: Allows user-defined functions to process each element in the array. arsort: Sort the array values ​​from large to small. asort: Sort the values ​​of the array by...
Statement
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
php 怎么求2个数组相同的元素php 怎么求2个数组相同的元素Dec 23, 2022 am 10:04 AM

php求2个数组相同元素的方法:1、创建一个php示例文件;2、定义两个有相同元素的数组;3、使用“array_intersect($array1,$array2)”或“array_intersect_assoc()”方法获取两个数组相同元素即可。

c语言数组如何初始化c语言数组如何初始化Jan 04, 2023 pm 03:36 PM

C语言数组初始化的三种方式:1、在定义时直接赋值,语法“数据类型 arrayName[index] = {值};”;2、利用for循环初始化,语法“for (int i=0;i<3;i++) {arr[i] = i;}”;3、使用memset()函数初始化,语法“memset(arr, 0, sizeof(int) * 3)”。

用Python实现动态数组:从入门到精通用Python实现动态数组:从入门到精通Apr 21, 2023 pm 12:04 PM

Part1聊聊Python序列类型的本质在本博客中,我们来聊聊探讨Python的各种“序列”类,内置的三大常用数据结构——列表类(list)、元组类(tuple)和字符串类(str)的本质。不知道你发现没有,这些类都有一个很明显的共性,都可以用来保存多个数据元素,最主要的功能是:每个类都支持下标(索引)访问该序列的元素,比如使用语法Seq[i]​。其实上面每个类都是使用数组这种简单的数据结构表示。但是熟悉Python的读者可能知道这3种数据结构又有一些不同:比如元组和字符串是不能修改的,列表可以

c++数组怎么初始化c++数组怎么初始化Oct 15, 2021 pm 02:09 PM

c++初始化数组的方法:1、先定义数组再给数组赋值,语法“数据类型 数组名[length];数组名[下标]=值;”;2、定义数组时初始化数组,语法“数据类型 数组名[length]=[值列表]”。

javascript怎么给数组中增加元素javascript怎么给数组中增加元素Nov 04, 2021 pm 12:07 PM

增加元素的方法:1、使用unshift()函数在数组开头插入元素;2、使用push()函数在数组末尾插入元素;3、使用concat()函数在数组末尾插入元素;4、使用splice()函数根据数组下标,在任意位置添加元素。

php怎么判断数组里面是否存在某元素php怎么判断数组里面是否存在某元素Dec 26, 2022 am 09:33 AM

php判断数组里面是否存在某元素的方法:1、通过“in_array”函数在数组中搜索给定的值;2、使用“array_key_exists()”函数判断某个数组中是否存在指定的key;3、使用“array_search()”在数组中查找一个键值。

go语言中元组是什么go语言中元组是什么Dec 27, 2022 am 11:27 AM

元组是固定长度不可变的顺序容器(元素序列),go语言中没有元组类型,数组就相当于元组。在go语言中,数组是一个由固定长度的特定类型元素组成的序列,一个数组可以由零个或多个元素组成;数组的声明语法为“var 数组变量名 [元素数量]Type”。

php 怎么去除第一个数组元素php 怎么去除第一个数组元素Dec 23, 2022 am 10:38 AM

php去除第一个数组元素的方法:1、新建一个php文件,并创建一个数组;2、使用“array_shift”方法删除数组首个元素;3、通过“print_”r输出数组即可。

See all articles

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft