PHP支持两种数组:索引数组(index array)和联合数组(associative array),索引数组使用数字作为键,联合数组使用字符串作为键。
函数名称:print_r
函数语法:bool print_r(mixed expression)
函数功能:将数组结构进行输出。
遍历数组的方法:
1)使用foreach结构遍历数组。
foreach(&url as $link){
echo $link.'<br>';
2)使用list结构遍历数组。
while(list($name,$value)=each($_POST)).....
字符串与数组的转换
1)使用explode()函数将字符串转换成数组
函数名称:explode
函数语法:array explode(string separator, string string, [int limit])
函数功能:将字符串依指定的字符串或字符separator切开。
2)使用implode()函数将数组转换成一个新字符串
函数名称:implode
函数语法:string implode(string glue, array pieces)
函数功能:将数组的内容组合成一个字符串
统计数组元素个数
函数名称:count
函数语法:int count(mixed array [, int mode])
函数功能:对数组中的元素个数进行统计。array为必要参数,输入的数组。mode为可选参数,COUNT_RECURSIVE(或1),如选中此参数,函数将递归地对数组计数。对计算多维数组的所有单元尤其有用。
查寻数组中指定元素
函数名称:array_search()
函数语法:mixed array_search(mixed needle, array haystack [, bool strict])
函数功能:在数组中搜索给定的值,找到后返回键名。needle指定在数组中搜索的值,haystack指定被搜索的数组,strict为可选参数,如果值为true,还将在数组中检查给定值的类型。(最常用在购物车)
获取数组中最后一个元素
函数名称:array_pop
函数语法:mixed array_pop(array array)
函数功能:获取并返回数组的最后一个元素,并将数组的长度减1,如果数组为空(或者不是数组)将返回null
向数组中添加元素
函数名称:array_push
函数语法:int array_push(array array, mixed var [, mixed ...])
函数功能:将变量压入数组的末尾,返回数组新的元素总数
删除数组中重复元素
函数名称:array_unique
函数语法:array array_unique(array array)
函数功能:删除数组中重复的元素