search
Homephp教程php手册数组处理函数库第1/2页
数组处理函数库第1/2页Jun 13, 2016 pm 12:36 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";
}
?>

参考 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
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.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),