list()函数可以一次性提取数组内多个值,同时为多个变量赋值.
cat data.txt
lizeng,male,24
zhangwenguo,male,27
liubing,male,27
-----------------------------------------------------------
$file=fopen("data.txt","r") or die("打开文件失败");
while($detail=fgets($file,4096)){
list($name,$***,$age)=explode(",",$detail);
printf("name is %s,***=%s,age=%s.
",$name,$***,$age)
?>
//返回
name is lizeng,***=male,age=24 .
name is zhangwenguo,***=male,age=27 .
name is liubing,***=male,age=27.
============================================================
range()函数返回一个包含此范围内所有整数的数组.
array range(int low,int high[,int step])
$arr=range(0,6);等同于
$arr=array(0,1,2,3,4,5,6)
打印数组用print_r($arr);不能用echo
============================================================
boolean is_array(mixed variable)判断某特定变量是否为一个数组
============================================================
int array_unshift(要添加元素的数组,被添加的元素[,被添加的元素]) 在数组头添加元素 返回的是数组的个数
例
$states=array("li","zhang");
$add=array_unshift($states,"guan","zhang");
print_r($states);
echo "
";
print_r($add);
//返回
Array ( [0] => guan [1] => zhang [2] => li [3] => zhang )
4
============================================================
int array_push(要添加元素的数组,被添加的元素[,被添加的元素]) 在数组尾添加元素
返回的是数组的个数
例同上
============================================================
mix array_shift(需要删除第一个元素的数组) 返回的是被删除的元素
例:
$des=array_shift($states);
print_r($des);
echo "
";
print_r($states)
//返回
guan
Array ( [0] => zhang [1] => li [2] => zhang )
============================================================
mix array_pop(需要删除最后一个元素的数组)
例子同上
============================================================
in_array()函数在数组中搜索一个特定的值,如果找到则返回true,否则返回false
例:
$pan=in_array("guan",$states);
if($pan)
echo "guan exists";
else
echo "guan doesn't exist"
//返回guan doesn't exist
============================================================
array_search()函数在一个数组中搜索一个指定的值.如果找到,则返回相应的键,如果找不到,则返回false
例
$found=array_search("zhang",$states);
print_r ($found."
");
//返回zhang的键值
============================================================
获取数组键
array_keys()函数返回一个数组,其中包含所搜索数组中找到的值
例
$state["xingming"]="lizeng";
$state1=array_keys($state);
echo "
";
print_r($state1);
//返回
Array ( [0] => xingming )
============================================================
获取数组值
$state2=array_values($state);
print_r($state2)
//返回
Array ( [0] => lizeng )
============================================================
获取当前数组键
key(array)
通过轮训可以输出数组所有的数组键
============================================================
获取当期的数组值
currunt(array)
通过轮训可以输出数组所有的数组值
============================================================
each(array)
通过轮训返回的数组包含4个键,键0和key包含键名,而键1和键value包含相应的值
例
while($fruit=each($states)){
print_r($fruit);
echo "
";
}
//返回
Array ( [1] => zhang [value] => zhang [0] => 0 [key] => 0 )
Array ( [1] => li [value] => li [0] => 1 [key] => 1 )
Array ( [1] => zhang [value] => zhang [0] => 2 [key] => 2 )
============================================================
next()将指针移动到下一个位置
例
$fui=next($states);
print_r($fui);
//返回li,第二个值
pre()返回位于当前指针上一个数组值,如果指针本来就是第一个,则返回false
reset(array)用于将数组指针设置回原来的数组值
end()用于将数组指针设置到最后一个数组值
============================================================
count()返回数组中值得总数
例:
$garden=array("qwer","asdf","zxcv");
echo "
";
echo count($garden);
//返回3
============================================================
array_count_values(array)函数返回一个数组,内容是array里值出现的频率
例
$states=array("zhang","li","zhang");
print_r(array_count_values($states));
//返回
Array ( [zhang] => 2 [li] => 1 )
============================================================
array_unique(array)函数返回一个数组,内容是array去重显示
例
print_r(array_unique($states))
//返回
Array ( [0] => zhang [1] => li )
============================================================
array_reverse(array)函数返回一个数组,顺序和array相反
例
$st=array("zhang","li","wang","liu");
print_r(array_reverse($st));
//返回
Array ( [0] => liu [1] => wang [2] => li [3] => zhang )
============================================================
array_flip()函数将置换数组的键值角色
例
print_r(array_flip($st))
//返回
Array ( [zhang] => 0 [li] => 1 [wang] => 2 [liu] => 3 )
============================================================
sort(array)函数对数组值进行从大到小排序,但是会破坏键值关系,如果保持键值关系,用asort()函数,如果相反的顺序,相对于sort(),用rsort()函数,但是也是破坏键值关系,如果不破坏键值关系,用arsort().
============================================================
合并数组
array_merge(array1,array2)函数将数组合并到一起,返回一个联合的数组
如果有重复的key,后面的值覆盖前面的值
例
$class1=array("john"=>100,"jack"=>50);
$class2=array("li"=>100,"john"=>80);
print_r(array_merge($class1,$class2));
//返回
Array ( [john] => 80 [jack] => 50 [li] => 100 )
array_merge_recursive(array1,array2)和array_merge()函数功能一样,唯一不同的是当出现重复键时,重复键会再生成一个数组
例
$class1=array("john"=>100,"jack"=>50);
$class2=array("li"=>100,"john"=>80);
print_r(array_merge_recursive($class1,$class2));
//返回
Array ( [john] => Array ( [0] => 100 [1] => 80 ) [jack] => 50 [li] => 100 )
============================================================
array_combine(array1,array2)函数会得到一个新的数组,注意,两个数组大小相等,不能为空
例
$class1=array("john"=>100,"jack"=>50);
$class2=array("li"=>100,"john"=>80);
print_r(array_combine($class1,$class2));
//返回
Array ( [100] => 100 [50] => 80 )
============================================================
array_slice(array,int offset[,int length]),拆分数组元素,数组键从0开始,从键offset开始(包括offset),到第offset+length个结束(不包含offset+length这个元素)
例
$slice=array("111","222","333","444","555","666","777");
print_r(array_slice($slice,2,4));
//返回
Array ( [0] => 333 [1] => 444 [2] => 555 [3] => 666 )
============================================================
array_splice(array,int offset[,int length[,array replacement]])
会删除数组中offset到offset+length结束的所有元素
例
$slice=array("111","222","333","444","555","666","777");
print_r(array_splice($slice,3));
echo "
";
print_r($slice);
//返回
Array ( [0] => 444 [1] => 555 [2] => 666 [3] => 777 )
Array ( [0] => 111 [1] => 222 [2] => 333 )
---------------------------------------------
$slice=array("111","222","333","444","555","666","777");
print_r($slice);
echo "
";
print_r(array_splice($slice,4,-1,array("888","999")));
echo "
";
print_r($slice)
//返回
Array ( [0] => 111 [1] => 222 [2] => 333 [3] => 444 [4] => 555 [5] => 666 [6] => 777 )
Array ( [0] => 555 [1] => 666 )
Array ( [0] => 111 [1] => 222 [2] => 333 [3] => 444 [4] => 888 [5] => 999 [6] => 777 )
============================================================
array_intersect(array1,array2,array3....)函数返回3个数组交集的元素所组成的数组,键保留第一个数组中的键.
array_intersect_assoc(array1,array2,array3....)函数返回关联数组的交集,不仅元素值一样,而且元素所对应的键也一样.
array_diff(array1,array2....)函数返回数组中值不一样的元素所组成的交集,和array_intersect()一样
array_diff_assoc(array1,array2....)函数返回数组中值且键不一样的元素的所组成的交集
============================================================
mix array_rand(array,[,int num])函数返回一个或多个键,这个是对键随机,如果忽略num参数,则只返回一个随机值,如果num>1,则返回一个由键所组成的数组
例
$class1=array("john"=>"100","jack"=>"50");
$class2=array("li"=>"100","john"=>"80");
$class3=array_merge_recursive($class1,$class2);
print_r(array_rand($class3,1));
print_r(array_rand($class3,3));
//返回
john
Array ( [0] => john [1] => li [2] => jack )
由于是随机值,可能每次执行都不一样
============================================================
void shuffle(array)这个是对元素值进行随机,更改键值对应关系 void类型将返回一个值.这样的函数会对数组进行更改
例
shuffle($class3);
print_r($class3);
//返回
Array ( [0] => 100 [1] => Array ( [0] => 100 [1] => 80 ) [2] => 50 )
mixed array_sum()函数对数组中的值进行求和
如果数组中包含其他数据类型,这些值将被忽略
============================================================
array array_chunk(array,int size[,boolean preserve_keys])函数将数组分解为一个多维数组,他有包含size个元素的数组所组成.
如果输入的数组无法按size均匀的划分,则最后一个数组中包含的元素将少于size.启用可选参数preserve_keys,将保持所对应的键值.
例子 1"Cat","b"=>"Dog","c"=>"Horse","d"=>"Cow");
print_r(array_chunk($a,2));
?>
输出:
Array (
[0] => Array ( [0] => Cat [1] => Dog )
[1] => Array ( [0] => Horse [1] => Cow )
)
例子 2"Cat","b"=>"Dog","c"=>"Horse","d"=>"Cow");
print_r(array_chunk($a,2,true));
?>
输出:
Array (
[0] => Array ( [a] => Cat [b] => Dog )
[1] => Array ( [c] => Horse [d] => Cow )
)
阅读(1438) | 评论(0) | 转发(0) |