动态网页技术PHP的数组处理函数库
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);
返回值: 无
函数种类: 资料处理
内容说明
本函数将数组中的元素依索引排序,排序后的索引和值仍然对应
使用范例
$fruits = array("d"=>"lemon","a"=>"orange","b"=>"banana","c"=>"apple");
ksort($fruits);
for(reset($fruits); $key = key($fruits); next($fruits)) {
echo "fruits[$key] = ".$fruits[$key]."\n";
}
?>
上面的例子,返回的资料为
fruits[a] = orange
fruits[b] = banana
fruits[c] = apple
fruits[d] = lemon
参考 arsort() asort() sort() rsort()
list
列出数组中元素的值。
语法: void list(...);
返回值: 无
函数种类: 资料处理
内容说明 和 array 函数一样,不算是正规的函数。list() 函数用在列出所有数组中的数值。
使用范例
下面的范例列出从 mysql 数据库返回的数组资料。
员工姓名 | 薪资 |
---|---|
$name | \n".$salary | \n".
参考 each() array()
next
将数组的内部指针向后移动。
语法: mixed next(array array);
返回值: 混合类型资料
函数种类: 资料处理
内容说明 这个函数返回数组下个元素的内部指针,若没有下个元素,则返回 false 值。当然,若是一个空的数组,它也返回 false 值。要正确地处理可能是空的数组,可以使用 each() 函数。此函数看起来像 current() 函数,不同地方在于本函数使指针往后指到下一个元素,并返回新的指针处的元素值,意即指针后移,并返回下一个元素值。
参考 current() end() prev() reset()
pos
返回数组目前的元素。
语法: mixed pos(array array);
返回值: 混合类型资料
函数种类: 资料处理
内容说明
此函数其实就是 current() 函数。
参考
end() next() prev() reset()
数组处理函数库
prev
将数组的内部指针往前移动。
语法: mixed prev(array array);
返回值: 混合类型资料
函数种类: 资料处理
内容说明 这个函数返回数组前一个元素的内部指针,若没有前一个元素,则返回 false 值。当然,若是一个空的数组,它也返回 false 值。要正确地处理可能是空的数组,可以使用 each() 函数。此函数有点像 next() 函数,只是 prev() 函数是将指针往前移,而 next() 函数是将指针往后移。
参考 current() end() next() reset()
range
建立一个整数范围的数组。
语法: array range(int low, int high);
返回值: 数组
函数种类: 资料处理
内容说明 此函数建立一个由小到大的连续整数数组。在 shuffle() 函数有使用范例
参考 shuffle()
reset
将数组的指针指到数组第一个元素。
语法: mixed reset(array array);
返回值: 混合类型资料
函数种类: 资料处理
内容说明 本函数将数组的指针重设,将指针改指到数组的第一个元素。返回值的内容也是数组的第一个元素。
使用范例
技巧: kk@shonline.de 指出 (30-jul-1998),当数组的资料不够稳定时,使用 reset() 会有警告信息。最常发生的时候是处理 html 表单(form)的输入资料时,若用户输入的资料不够充份,或用户故意增删输入 (input) 字段,就有可能会有警告信息。用 @reset() 的方式可使警告信息不出现 (在 reset() 前加 @),不过这只是掩耳盗铃的方式;要斧底抽薪的方法是使用 if (isset()) 的方式 (参见 4.48.14 isset() 函数),如下例
if (isset($form_array)) {
reset($form_array);
while (list($k, $v) = each($form_array)) {
do_something($k, $v);
}
}
?>
参考 current() each() next() prev()
rsort
将数组的值由大到小排序。
语法: void rsort(array array);
返回值: 无
函数种类: 资料处理
内容说明
本函数将数组依由大到小的方式重排。
使用范例
$fruits = array("lemon","orange","banana","apple");
rsort($fruits);
for(reset($fruits); list($key,$value) = each($fruits); ) {
echo "fruits[$key] = ".$value."\n";
}
?>
处理后的结果为
fruits[0] = orange
fruits[1] = lemon
fruits[2] = banana
fruits[3] = apple
参考 arsort() asort() ksort() sort() usort()
shuffle
将数组的顺序弄混。
语法: void shuffle(array array);
返回值: 无
函数种类: 资料处理
内容说明 本函数将数组以随机的方式使全部元素重新乱排。
使用范例
$numbers = range(1,20);
srand(time());
shuffle($numbers);
while (list(,$number) = each($numbers)) {
echo "$number ";
}
?>
注意:
ccunning@math.ohio-state.edu 指出 (31-may-1999),在 solaris 上由于受了 libc 函数库的影响,会使本函数出现问题。目前解决的方式如下面的例子。(注: 或许之后新的 php 版本会修正这个问题。)
function myshuffle($array) {
mt_srand((double) microtime()*1000000);
$num = count($array);
for ($i=0; $i$n = mt_rand($i+1,$num);
$temp = $array[$n];
$array[$n] = $array[$i];
$array[$i] = $temp;
}
}
$array = array(1,2,3,4,5,6,7,8,9,10);
myshuffle(&$array);
while (list(,$var)=each($array)) {
echo $var . " ";
}
?>
参考 arsort() asort() ksort() rsort() sort() usort()
sizeof
获知数组的大小。
语法: int sizeof(array array);
返回值: 整数
函数种类: 资料处理
内容说明
本函数以整数类型返回数组的元素个数。
参考 count()
sort
将数组排序。
语法: void sort(array array);
返回值: 无
函数种类: 资料处理
内容说明 本函数将数组依由小到大的方式重排。
使用范例
$fruits = array("lemon","orange","banana","apple");
sort($fruits);
for(reset($fruits); list($key,$value) = each($fruits); ) {
echo "fruits[$key] = ".$value."\n";
}
?>
返回的结果为
fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange
参考 arsort() asort() ksort() rsort() usort()
uasort
将数组依用户自定的函数排序。
语法: void uasort(array array, function cmp_function);
返回值: 无
函数种类: 资料处理
内容说明 本函数将数组依用户自定的方式重排,当然元素的索引和值仍然保持对应的关系。当您觉得需要自行设计排序的方式时,可以先自订处理函数,再使用本函数将数组值排序。
参考 arsort() asort() ksort() rsort() sort() uksort()
uksort
将数组的索引依用户自定的函数排序。
语法: void uksort(array array, function cmp_function);
返回值: 无
函数种类: 资料处理
内容说明 本函数将数组的索引依用户自定的方式重排,cmp_function 即用户自行开发的函数。
使用范例
function mycompare($a, $b) {
if ($a == $b) return 0;
return ($a > $b) ? -1 : 1;
}
$a = array(4 => "four", 3 => "three", 20 => "twenty", 10 => "ten");
uksort($a, mycompare);
while(list($key, $value) = each($a)) {
echo "$key: $value\n";
}
?>
上面的程序返回资料为
20: twenty
10: ten
4: four
3: three
参考 arsort() asort() ksort() rsort() sort() uasort()
usort
将数组的值依用户自定的函数排序。
语法: void usort(array array, function cmp_function);
返回值: 无
函数种类: 资料处理
内容说明
本函数将数组的值依用户自定的方式重排。若程序中需要使用特殊的排序方式,则可以使用这个函数。
使用范例
function cmp($a,$b) {
if ($a == $b) return 0;
return ($a > $b) ? -1 : 1;
}
$a = array(3,2,5,6,1);
usort($a, cmp);
while(list($key,$value) = each($a)) {
echo "$key: $value\n";
}
?>
此例的返回值为
0: 6
1: 5
2: 3
3: 2
4: 1
参考 arsort() asort() ksort() rsort() sort()

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety
