Heim  >  Artikel  >  Backend-Entwicklung  >  PHP5中数组函数总结篇_PHP教程

PHP5中数组函数总结篇_PHP教程

WBOY
WBOYOriginal
2016-07-13 17:38:20805Durchsuche

我不知道PHP初学者是怎么学习函 数的,但是我认为学习他一定是有目的性,它是目的驱动的,以数组函数为例!这么多的数组函数我们该怎么学习?其实我们只需要清楚,我们需要对数组做哪些操 作,然后把这些操作归类,心中就有了大概的印象,然后随着你的实践,这些函数你就很清楚了,在这里提醒大家的是手册要常备!废话不多说,数组函数一般归为 以下几类(看到英文别怕哦):
Outputting arrays
Creating arrays
Testing for an array
Locating array elements
Traversing arrays
Determining array size and element uniqueness
Sorting arrays
Merging, slicing, splicing, and dissecting arrays

Outputting arrays
print_r()
不熟悉这个的我就不想多说了!最基本的函数,当然要输出数组也可是使用循环结构输出,不过有这么方便的输出函数干吗不用呢!

Creating arrays
array()
这个函数我也不想说了,这其实只能算语言结构,大家在新建数组的时候都用到过,没用过的我只能说,兄弟,我无语了!
list()
这个函数和上面提到的array()一样,只是个语言结构,它通过一步操作给一组变量赋值!
具体示例请查看手册!
range();
array range ( mixed low, mixed high[, number step] )
建立一个包含指定范围单元的数组
示例:$arr =range(0, 6);
相当于$arr =array(0, 1, 2, 3, 4, 5, 6);
一般数据按从低到高排列,如果low> high,则从高到低排列;

Testing for an array
is_array();
测试变量是否array类型,是则返回true,不是则返回false,很基本,很实用的函数!

Adding and removing array elements
array_push()
int array_push ( array &target_array,mixed var [, mixed ...] )
将一个或多个单元从末尾添加至数组!返回新数组的单元总数!
示例:

复制PHP内容到剪贴板
PHP代码:
<font face="新宋体">$states <span style="color: rgb(0,119,0)">=array(</span><span style="color: rgb(0,0,187)">‘Ohio’</span><span style="color: rgb(0,119,0)">, </span><span style="color: rgb(0,0,187)">‘New York’</span></font><font face="新宋体"><span style="color: rgb(0,119,0)">);<br> </span><span style="color: rgb(0,0,187)">array_push</span><span style="color: rgb(0,119,0)">(</span><span style="color: rgb(0,0,187)">$states</span><span style="color: rgb(0,119,0)">, </span><span style="color: rgb(0,0,187)">‘California’</span><span style="color: rgb(0,119,0)">,</span><span style="color: rgb(0,0,187)">‘Texas’</span></font><font face="新宋体"><span style="color: rgb(0,119,0)">);<br> </span><span style="color: rgb(255,128,0)">//output: array((‘Ohio’,‘New York’ , ‘California’,‘Texas’);</span><br> <br> </font>

array_pop();
弹出并返回数组的最后一个单元,并将数组长度减一。使用后会重置数组指针!
示例:

复制PHP内容到剪贴板
PHP代码:
<font face="新宋体">$states <span style="color: rgb(0,119,0)">=array(</span><span style="color: rgb(0,0,187)">‘Ohio’</span><span style="color: rgb(0,119,0)">, </span><span style="color: rgb(0,0,187)">‘New York’</span></font><font face="新宋体"><span style="color: rgb(0,119,0)">);<br> </span><span style="color: rgb(0,0,187)">$state </span><span style="color: rgb(0,119,0)">= </span><span style="color: rgb(0,0,187)">array_pop</span><span style="color: rgb(0,119,0)">(</span><span style="color: rgb(0,0,187)">$states</span></font><font face="新宋体"><span style="color: rgb(0,119,0)">);<br> </span><span style="color: rgb(255,128,0)">//output: New York</span><br> <br> </font>

array_shift();
与array_pop类似,只不过它移出和返回的是数组的第一个值。数字键名将重新从零开始记数,文字键名则保持不变!

array_unshift();
与array_push类似,只不过是从数组头部插入数据!数字键名将重新从零开始记数,文字键名则保持不变!

array_pad();
array array_pad ( array input, intpad_size, mixed pad_value )
用值将数组填充到指定长度!
pad_size为正,则从右侧开始填补,为负,从左侧开始填补,小与数组长度,则保持数组不变!示例见手册!

Locating array elements
in_array();
检查数组中是否存在某个值,基础,不说了,具体见手册!

array_keys();
array array_keys ( array input [,mixed search_value [, bool strict]] )
返回数组中的所有键名。如果选定search_value,则返回选定的键名!自PHP 5]起,可以用strict参数来进行全等比较(===)。

array_key_exists();
bool array_key_exists ( mixed key,array search )
检查给定的键名或索引是否存在于数组中,存在则返回true。一些更具体的应用记得查看手册!

array_values();
array array_values ( array input )
与array_keys()类似!返回的是数组的所有键值!

array_search();
mixed array_search ( mixed needle,array haystack [, bool strict] )
在 haystack中搜索 needle参数并在找到的情况下返回键名,否则返回FALSE
如果needle是字符串,则比较区分大小写!
如果strict是true,还得比较类型
如果needle在haystack中出现不止一次,则返回第一个匹配的键。要返回所有匹配值的键,应该用array_keys()加上可选参数search_value来代替!

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/486501.htmlTechArticle我不知道 PHP 初学者是怎么 学习 函 数的,但是我认为学习他一定是有目的性,它是目的驱动的,以数组函数为例!这么多的数组函数我们...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn