最新消息: 新版网站上线了!!!

PHP函数查询参考手册之Array篇

  • 摘自http://www.w3school.com.cn/php/php_functions.asp
     
    array(key => value)创建数组
    $a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
    print_r($a);//Array ( [a] => Dog [b] => Cat [c] => Horse )
    $a=array("Dog","Cat","Horse");
    print_r($a);//Array ( [0] => Dog [1] => Cat [2] => Horse )
     
    array_change_key_case(array,case)将KEY转换大小写
    $a=array("a"=>"Cat","b"=>"Dog","c"=>"Horse");
    print_r(array_change_key_case($a,CASE_UPPER));//Array ( [A] => Cat [B] => Dog [C] => Horse )      默认值:CASE_LOWER
     
    array_chunk(array,size,preserve_key)分割数组为新的数组块
    $a=array("a"=>"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 ) )
    $a=array("a"=>"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 ) )
     
    array_combine(array1,array2)合并数组
    $a1=array("a","b","c","d");
    $a2=array("Cat","Dog","Horse","Cow");
    print_r(array_combine($a1,$a2));//Array ( [a] => Cat [b] => Dog [c] => Horse [d] => Cow )
     
    array_count_values(array)统计数组中所有的值出现的次数
    $array = array(1, "hello", 1, "world", "hello");
    print_r(array_count_values ($array));//Array ( [1] => 2 [hello] => 2 [world] => 1 )
     
    array_diff(array1,array2,array3...)返回两个数组的差集数组
    :值比较
    $a1 = array(0=>"Cat", 1=>"Dog", 2=>"Horse");
    $a2 = array(3=>"Horse", 4=>"Dog", 5=>"Fish");
    print_r(array_diff($a1, $a2));//Array ( [0] => Cat )
     
    array_diff_assoc(array1,array2,array3...)返回两个数组的差集数组,和array_diff()不同,本函数要求键名和键值都进行比较。返回的数组中键名保持不变
    $a1=array(0=>"Cat",1=>"Dog",2=>"Horse");
    $a2=array(0=>"Rat",1=>"Horse",2=>"Dog");
    $a3=array(0=>"Horse",1=>"Dog",2=>"Cat");
    print_r(array_diff_assoc($a1,$a2,$a3));//Array ( [0] => Cat [2] => Horse )
     
    array_diff_key(array1,array2,array3...)返回一个数组,该数组包括了所有在被比较的数组中,但是不在任何其他参数数组中的键
    :键名比较
    $a1=array(0=>"Cat",1=>"Dog",2=>"Horse");
    $a2=array(2=>"Bird",3=>"Rat",4=>"Fish");
    $a3=array(5=>"Horse",6=>"Dog",7=>"Bird");
    print_r(array_diff_key($a1,$a2,$a3));//Array ( [0] => Cat [1] => Dog )
     
    array_diff_uassoc(array1,array2,array3...,function) 使用用户自定义的回调函数做索引检查来计算两个或多个数组的差集。返回一个数组,该数组包括了在 array1 中但是不在任何其他参数数组中的值。
    function myfunction($v1,$v2) {
    if ($v1===$v2){return 0;}
    if ($v1>$v2){return 1;}
    else{return -1;}
    }
    $a1=array(0=>"Dog",1=>"Cat",2=>"Horse");
    $a2=array(3=>"Dog",1=>"Cat",5=>"Horse");
    print_r(array_diff_uassoc($a1,$a2,"myfunction"));//Array ( [0] => Dog [2] => Horse )
    分配多个数组
    $a1=array(0=>"Dog",1=>"Cat",2=>"Horse");
    $a2=array(3=>"Dog",1=>"Cat",5=>"Horse");
    $a3=array(6=>"Bird",0=>"Dog",5=>"Horse");
    print_r(array_diff_uassoc($a1,$a2,$a3,"myfunction"));//Array ( [2] => Horse )
     
    array_diff_ukey(array1,array2,array3...,function)返回一个数组,该数组包括了所有出现在 array1 中但是未出现在任何其它参数数组中的键名的值
    function myfunction($v1,$v2) {
    if ($v1===$v2){return 0;}
    if ($v1>$v2){return 1;}
    else{return -1;}
    }
    $a1=array(0=>"Dog",1=>"Cat",2=>"Horse");
    $a2=array(3=>"Rat",1=>"Bird",5=>"Monkey");
    print_r(array_diff_ukey($a1,$a2,"myfunction"));//Array ( [0] => Dog [2] => Horse )
    分配多个数组:
    $a1=array(0=>"Dog",1=>"Cat",2=>"Horse");
    $a2=array(3=>"Rat",1=>"Bird",5=>"Monkey");
    $a3=array(6=>"Dog",7=>"Donkey",0=>"Horse");
    print_r(array_diff_ukey($a1,$a2,$a3,"myfunction"));//Array ( [2] => Horse )
     
    array_fill(start,number,value)用给定的值填充数组,返回的数组有 number 个元素,值为 value,返回的数组使用数字索引,从 start 位置开始并递增。如果 number 为 0 或小于 0,就会出错
    start 必需。数值,规定键的起始索引
    number 必需。数值,规定填充的数量,其值必须大于0
    value 必需。规定要插入的值
    $a=array_fill(2,5,"Dog");
    print_r($a);//Array ( [2] => Dog [3] => Dog [4] => Dog [5] => Dog [6] => Dog )
     
    array_filter(array,function)用回调函数过滤数组中的元素,如果自定义过滤函数返回 true,则被操作的数组的当前值就会被包含在返回的结果数组中, 并将结果组成一个新的数组。如果原数组是一个关联数组,键名保持不变。
    function myfunction($v) {
    if ($v==="Horse"){return true;}
    return false;
    }
    $a=array(0=>"Dog",1=>"Cat",2=>"Horse");
    print_r(array_filter($a,"myfunction"));//Array ( [2] => Horse )
     
    array_flip(array)返回一个反转后的数组[键值交换],如果同一值出现了多次,则最后一个键名将作为它的值,所有其他的键名都将丢失
    $a=array(0=>"Dog",1=>"Cat",2=>"Horse");print_r(array_flip($a));// Array ( [Dog] => 0 [Cat] => 1 [Horse] => 2 )
     
    array_intersect(array1,array2,array3...)返回两个或多个数组的交集数组
    $a1=array(0=>"Cat",1=>"Dog",2=>"Horse");
    $a2=array(3=>"Horse",4=>"Dog",5=>"Fish");
    print_r(array_intersect($a1,$a2));// Array ( [1] => Dog [2] => Horse )
     
    array_intersect_assoc(array1,array2,array3...)返回两个或多个数组的交集数组,与array_intersect()不同的是,本函数除了比较键值,还比较键名。返回的数组中元素的键名保持不变
    $a1=array(0=>"Cat",1=>"Dog",2=>"Horse");
    $a2=array(3=>"Horse",1=>"Dog",0=>"Cat");
    print_r(array_intersect_assoc($a1,$a2));// Array ( [0] => Cat [1] => Dog )
    $a1=array(0=>"Cat",1=>"Dog",2=>"Horse");
    $a2=array(3=>"Horse",1=>"Dog",5=>"Fish");
    $a3=array(6=>"Cow",1=>"Dog",8=>"Fish");
    print_r(array_intersect_assoc($a1,$a2,$a3));// Array ( [1] => Dog )
     
    array_intersect_key(array1,array2,array3...)使用键名比较计算数组的交集
    $a1=array(0=>"Cat",1=>"Dog",2=>"Horse");
    $a2=array(2=>"Bird",0=>"Cat",4=>"Fish");
    print_r(array_intersect_key($a1,$a2));// Array ( [0] => Cat [2] => Horse )
    $a1=array(0=>"Cat",1=>"Dog",2=>"Horse");
    $a2=array(2=>"Bird",3=>"Rat",4=>"Fish");
    $a3=array(2=>"Dog",6=>"Cow",7=>"Bird");
    print_r(array_intersect_key($a1,$a2,$a3));// Array ( [2] => Horse )
     
    array_intersect_uassoc(array1,array2,array3...,function)使用用户自定义的回调函数计算数组的交集,用回调函数比较索引,注意,与array_intersect()不同的是除了比较键值,还要比较键名
    function myfunction($v1,$v2) {
    if ($v1===$v2){return 0;}
    if ($v1>$v2){return 1;}
    else{return -1;}
    }
    $a1=array(0=>"Dog",1=>"Cat",2=>"Horse");
    $a2=array(3=>"Dog",1=>"Cat",5=>"Horse");
    print_r(array_intersect_uassoc($a1,$a2,"myfunction"));// Array ( [1] => Cat )
    分配多个数组
    function myfunction($v1,$v2) {
    if ($v1===$v2){return 0;}
    if ($v1>$v2){return 1;}
    else{return -1;}
    }
    $a1=array(0=>"Dog",1=>"Cat",2=>"Horse");
    $a2=array(0=>"Dog",1=>"Cat",5=>"Horse");
    $a3=array(6=>"Bird",0=>"Dog",5=>"Horse");
    print_r(array_intersect_uassoc($a1,$a2,$a3,"myfunction"));// Array ( [0] => Dog )
     
    array_key_exists(key,array)判断某个数组中是否存在指定的key,如果存在,则返回true,否则返回false。
    $a=array("a"=>"Dog","b"=>"Cat");
    if (array_key_exists("a",$a)){echo "Key exists!";}
    else{echo "Key does not exist!";}// Key exists!
     
    $a=array("Dog",”Cat");
    if (array_key_exists(0,$a)){echo "Key exists!";}
    else{echo "Key does not exist!";}// Key exists!
     
    array_keys(array,value)返回包含数组中所有键名的一个新数组
    $a=array("a"=>"Horse","b"=>"Cat","c"=>"Dog");
    print_r(array_keys($a,"Dog"));//Array ( [0] => c )
    使用 strict参数
    $a=array(10,20,30,"10");
    print_r(array_keys($a,"10",false));// Array ( [0] => 0 [1] => 3 )
    $a=array(10,20,30,"10");
    print_r(array_keys($a,"10",true));// Array ( [0] => 3 )
     
    array_map(function,array1,array2,array3...)返回用户自定义函数作用后的数组。回调函数接受的参数数目应该和传递给 array_map() 函数的数组数目一致。
    function myfunction($v)
    {
    if ($v==="Dog"){return "Fido";}
    return $v;
    }
    $a=array("Horse","Dog","Cat");
    print_r(array_map("myfunction",$a));// Array ( [0] => Horse [1] => Fido [2] => Cat )
    使用多个参数
    function myfunction($v1,$v2)
    {
    if ($v1===$v2){return "same";}
    return "different";
    }
    $a1=array("Horse","Dog","Cat");
    $a2=array("Cow","Dog","Rat");
    print_r(array_map("myfunction",$a1,$a2));// Array ( [0] => different [1] => same [2] => different )
    自定义函数名设置为 null 时的情况
    $a1=array("Dog","Cat");
    $a2=array("Puppy","Kitten");
    print_r(array_map(null,$a1,$a2));// Array ( [0] => Array ( [0] => Dog [1] => Puppy ) [1] => Array ( [0] => Cat [1] => Kitten ) )
     
    array_merge(array1,array2,array3...)把两个或多个数组合并为一个数组
    $a1=array("a"=>"Horse","b"=>"Dog");
    $a2=array("c"=>"Cow","b"=>"Cat");
    print_r(array_merge($a1,$a2));// Array ( [a] => Horse [b] => Cat [c] => Cow )
    仅使用一个数组参数
    $a=array(3=>"Horse",4=>"Dog");
    print_r(array_merge($a));// Array ( [0] => Horse [1] => Dog )
     
    array_merge_recursive(array1,array2,array3...)将一个或多个数组的元素的合并起来,一个数组中的值附加在前一个数组的后面。并返回作为结果的数组
    $a1=array("a"=>"Horse","b"=>"Dog");
    $a2=array("c"=>"Cow","b"=>"Cat");
    print_r(array_merge_recursive($a1,$a2));// Array ( [a] => Horse [b] => Array ( [0] => Dog [1] => Cat ) [c] => Cow )
     
    array_multisort(array1,sorting order,sorting type,array2,array3...)对多个数组或多维数组进行排序
    $a1=array("Dog","Cat");
    $a2=array("Fido","Missy");
    array_multisort($a1,$a2);
    print_r($a1);// Array ( [0] => Cat [1] => Dog )
    print_r($a2);// Array ( [0] => Missy [1] => Fido )
    带有排序参数
    $a1=array("Dog","Dog","Cat");
    $a2=array("Pluto","Fido","Missy");
    array_multisort($a1,SORT_ASC,$a2,SORT_DESC);
    print_r($a1);// Array ( [0] => Cat [1] => Dog [2] => Dog )
    print_r($a2);// Array ( [0] => Missy [1] => Pluto [2] => Fido )
     
    array_pad(array,size,value)向一个数组插入带有指定值的指定数量的元素
    $a=array("Dog","Cat");
    print_r(array_pad($a,5,0));// Array ( [0] => Dog [1] => Cat [2] => 0 [3] => 0 [4] => 0 )
    带有负的 size 参数
    $a=array("Dog","Cat");
    print_r(array_pad($a,-5,0));// Array ( [0] => 0 [1] => 0 [2] => 0 [3] => Dog [4] => Cat )
     
    array_pop()删除数组中的最后一个元素
    $a=array("Dog","Cat","Horse");
    array_pop($a);
    print_r($a);// Array ( [0] => Dog [1] => Cat )
     
    array_product(array)计算并返回数组中所有值的乘积
    $a=array(5,5);
    echo(array_product($a));// 25
     
    array_push(array,value1,value2...)向第一个参数的数组尾部添加一个或多个元素(入栈),然后返回新数组的长度。该函数等于多次调用 $array[] = $value。
    $a=array("Dog","Cat");
    array_push($a,"Horse","Bird");
    print_r($a);// Array ( [0] => Dog [1] => Cat [2] => Horse [3] => Bird )
    带有字符串键的数组
    $a=array("a"=>"Dog","b"=>"Cat");
    array_push($a,"Horse","Bird");
    print_r($a);// Array ( [a] => Dog [b] => Cat [0] => Horse [1] => Bird )
     
    array_rand(array,number)从数组中随机选出一个或多个元素,并返回
    第二个参数用来确定要选出几个元素。如果选出的元素不止一个,则返回包含随机键名的数组,否则返回该元素的键名
    $a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
    print_r(array_rand($a,1));// b à随机的
    带有字符串键的数组
    $a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
    print_r(array_rand($a,2));// Array ( [0] => a [1] => c ) à随机的
     
    array_reduce(array,function,initial)用回调函数迭代地将数组简化为单一的值
    function myfunction($v1,$v2)
    {
    return $v1 . "-" . $v2;
    }
    $a=array("Dog","Cat","Horse");
    print_r(array_reduce($a,"myfunction"));// -Dog-Cat-Horse
    带有 initial 参数
    function myfunction($v1,$v2)
    {
    return $v1 . "-" . $v2;
    }
    $a=array("Dog","Cat","Horse");
    print_r(array_reduce($a,"myfunction",5));// 5-Dog-Cat-Horse
    返回总和
    function myfunction($v1,$v2)
    {
    return $v1+$v2;
    }
    $a=array(10,15,20);
    print_r(array_reduce($a,"myfunction",5));// 50
     
    array_reverse(array,preserve)将原数组中的元素顺序翻转,创建新的数组并返回。如果第二个参数指定为 true,则元素的键名保持不变,否则键名将丢失。
    $a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
    print_r(array_reverse($a));// Array ( [c] => Horse [b] => Cat [a] => Dog )
     
    array_search(value,array,strict)在数组中查找一个键值。如果找到了该值,匹配元素的键名会被返回。如果没找到,则返回 false
    $a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
    echo array_search("Cat",$a);// b
     
    array_shift(array)删除数组中的第一个元素,并返回被删除元素的值
    $a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
    echo array_shift($a);// Dog
    print_r ($a);// Array ( [b] => Cat [c] => Horse )
    $a=array(0=>"Dog",1=>"Cat",2=>"Horse");
    echo array_shift($a); // Dog
    print_r ($a);// //Array ( [0] => Cat [1] => Horse )
     
    array_slice(array,offset,length,preserve)在数组中根据条件取出一段值,并返回
    $a=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
    print_r(array_slice($a,1,2));// Array ( [0] => Cat [1] => Horse )
    $a=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
    print_r(array_slice($a,-2,1));// Array ( [0] => Horse )
    $a=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
    print_r(array_slice($a,1,2,true));// Array ( [1] => Cat [2] => Horse )
    $a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse","d"=>"Bird");
    print_r(array_slice($a,1,2));// Array ( [b] => Cat [c] => Horse )
     
    array_splice(array,offset,length,array)与 array_slice() 函数类似,选择数组中的一系列元素,但不返回,而是删除它们并用其它值代替
    $a1=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
    $a2=array(0=>"Tiger",1=>"Lion");
    array_splice($a1,0,2,$a2);
    print_r($a1);// Array ( [0] => Tiger [1] => Lion [2] => Horse [3] => Bird )
    length 参数设置为 0
    $a1=array(0=>"Dog",1=>"Cat");
    $a2=array(0=>"Tiger",1=>"Lion");
    array_splice($a1,1,0,$a2);
    print_r($a1);// Array ( [0] => Dog [1] => Tiger [2] => Lion [3] => Cat )
     
    array_sum(array)返回数组中所有值的总和
    $a=array(0=>"5",1=>"15",2=>"25");
    echo array_sum($a);// 45
     
    array_udiff(array1,array2,array3...,function)返回一个数组,该数组包括了所有在被比较数组中,但是不在任何其它参数数组中的值,键名保留不变。
    注释:该函数只进行键值的比较,不比较键名, 仅检查多维数组中的一维
    function myfunction($v1,$v2)
    {
    if ($v1===$v2){return 0;}
    return 1;
    }
    $a1=array("a"=>"Cat","b"=>"Dog","c"=>"Horse");
    $a2=array(1=>"Cat",2=>"Dog",3=>"Fish");
    print_r(array_udiff($a1,$a2,"myfunction"));// Array ( [c] => Horse )
     
    array_udiff_assoc(array1,array2,array3...,function)返回 array1 中存在但其它数组中都不存在的部分
    function myfunction($v1,$v2)
    {
    if ($v1===$v2){return 0;}
    return 1;
    }
    $a1=array("a"=>"Cat","b"=>"Dog","c"=>"Horse");
    $a2=array("a"=>"Cat","b"=>"Horse","c"=>"Dog");
    print_r(array_udiff_assoc($a1,$a2,"myfunction"));// Array ( [b] => Dog [c] => Horse )
     
    array_udiff_uassoc(array1,array2,array3...,function1,function2)返回 array1 数组中存在但其它数组中都不存在的部分。返回的数组中键名保持不变。
    注释:function1 指定的函数用于比较键名是否相等。function2 指定的函数用于比较键值是否相等。
    function myfunction_key($v1,$v2)
    {
    if ($v1===$v2){return 0;}
    return 1;
    }
    function myfunction_value($v1,$v2)
    {
    if ($v1===$v2){return 0;}
    return 1;
    }
    $a1=array("a"=>"Cat","b"=>"Dog","c"=>"Horse");
    $a2=array("a"=>"Cat","b"=>"Dog","c"=>"Fish");
    print_r(array_udiff_uassoc($a1,$a2,"myfunction_key","myfunction_value"));// Array ( [c] => Horse )
     
    array_uintersect(array1,array2,array3...,function)计算数组的交集,用回调函数比较数据
    注释:只进行键值的比较,不比较键名, 返回的数组中键名保持不变
    function myfunction($v1,$v2)
    {
    if ($v1===$v2){return 0;}
    if ($v1 > $v2) return 1;{return -1;}
    return 1;
    }
    $a1=array("a"=>"Cat","b"=>"Dog","c"=>"Horse");
    $a2=array(1=>"Cat",2=>"Dog",3=>"Fish");
    print_r(array_uintersect($a1,$a2,"myfunction"));// Array ( [a] => Cat [b] => Dog )
     
    array_uintersect_assoc(array1,array2,array3...,function)带索引检查计算数组的交集,用回调函数比较数据,与array_uintersect()不同的是键名也要比较。数据(键值)是用回调函数比较的, 返回的数组中键名保持不变
    function myfunction($v1,$v2)
    {
    if ($v1===$v2){return 0;}
    return 1;
    }
    $a1=array("a"=>"Cat","b"=>"Dog","c"=>"Horse");
    $a2=array("a"=>"Cat","b"=>"Horse","c"=>"Dog");
    print_r(array_uintersect_assoc($a1,$a2,"myfunction"));// Array ( [a] => Cat )
     
    array_uintersect_uassoc(array1,array2,array3...,function1,function2)带索引检查计算数组的交集,与array_uintersect()用回调函数来比较数据和索引,不同的是键名也要比较。键值和键名(索引)都是用回调函数比较的
    function myfunction_key($v1,$v2)
    {
    if ($v1===$v2){return 0;}
    return 1;
    }
     
    function myfunction_value($v1,$v2)
    {
    if ($v1===$v2){return 0;}
    return 1;
    }
    $a1=array("a"=>"Cat","b"=>"Dog","c"=>"Horse");
    $a2=array("a"=>"Cat","b"=>"Dog","c"=>"Dog");
    print_r(array_uintersect_uassoc($a1,$a2,"myfunction_key","myfunction_value"));// Array ( [a] => Cat [b] => Dog )
     
    array_unique(array)移除数组中的重复的值,并返回结果数组,返回的数组中键名不变
    注释:被返回的数组将保持第一个数组元素的键类型
    $a=array("a"=>"Cat","b"=>"Dog","c"=>"Cat");
    print_r(array_unique($a));// Array ( [a] => Cat [b] => Dog )
     
    array_unshift(array,value1,value2,value3...)在数组开头插入一个或多个元素
    注释:所有的数值键名将修改为从零开始重新计数,所有的字符串键名保持不变
    $a=array("a"=>"Cat","b"=>"Dog",0=>”Snake”);
    array_unshift($a,"Horse");
    print_r($a);// Array ( [0] => Horse [a] => Cat [b] => Dog [1] => Snake )
    返回键值
    $a=array("a"=>"Cat","b"=>"Dog");
    print_r(array_unshift($a,"Horse"));//3
    数组带有数值键
    $a=array(0=>"Cat",1=>"Dog");
    array_unshift($a,"Horse");
    print_r($a);// Array ( [0] => Horse [1] => Cat [2] => Dog )
     
    array_values(array)返回一个包含给定数组中所有键值的数组,但不保留键名
    提示:被返回的数组将使用数值键,从 0 开始且以 1 递增
    $a=array("a"=>"Cat","b"=>"Dog","c"=>"Horse");
    print_r(array_values($a));// Array ( [0] => Cat [1] => Dog [2] => Horse )
     
    array_walk(array,function,userdata...)对数组中的每个元素应用回调函数。如果成功则返回 TRUE,否则返回 FALSE
    function myfunction($value,$key)
    {
    echo "The key $key has the value $value<br />";
    }
    $a=array("a"=>"Cat","b"=>"Dog","c"=>"Horse");
    array_walk($a,"myfunction");// The key a has the value Cat
    The key b has the value Dog
    The key c has the value Horse
    带有一个参数
    function myfunction($value,$key,$p)
    {
    echo "$key $p $value<br />";
    }
    $a=array("a"=>"Cat","b"=>"Dog","c"=>"Horse");
    array_walk($a,"myfunction","has the value");// a has the value Cat
    b has the value Dog
    c has the value Horse
    改变数组元素的值
    function myfunction(&$value,$key)
    {
    $value="Bird”;
    }
    $a=array("a"=>"Cat","b"=>"Dog","c"=>"Horse");
    array_walk($a,"myfunction");
    print_r($a);// Array ( [a] => Bird [b] => Bird [c] => Bird )
     
    array_walk_recursive(array,function,userdata)对数组中的每个元素应用回调函数与array_walk()不一样的是,如果原数组中的元素也是数组,就会递归地调用回调函数,也就是说,会递归到更深层的数组中去
    function myfunction($value,$key)
    {
    echo "The key $key has the value $value<br />";
    }
    $a1=array("a"=>"Cat","b"=>"Dog");
    $a2=array($a1,"1"=>"Bird","2"=>"Horse");
    array_walk_recursive($a2,"myfunction");// The key a has the value Cat
    The key b has the value Dog
    The key 1 has the value Bird
    The key 2 has the value Horse
     
    arsort(array,sorttype)对数组进行逆向排序并保持索引关系
    asort(array,sorttype)对数组进行顺序排序并保持索引关系
    sorttype
    SORT_REGULAR - 默认。以它们原来的类型进行处理(不改变类型)
    SORT_NUMERIC - 把值作为数字来处理
    SORT_STRING - 把值作为字符串来处理
    SORT_LOCALE_STRING - 把值作为字符串来处理,基于本地设置
    $my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
    arsort($my_array);
    print_r($my_array);// Array ( [c] => Horse [a] => Dog [b] => Cat )
    asort($my_array);
    print_r($my_array);// Array ( [b] => Cat [a] => Dog [c] => Horse )
     
    compact(var1,var2...创建一个由参数所带变量组成的数组。如果参数中存在数组,该数组中变量的值也会被获取
    注释:任何没有变量名与之对应的字符串都被略过
    $firstname = "Peter";
    $lastname = "Griffin";
    $age = "38";
    $result = compact("firstname", "lastname", "age");
    print_r($result);// Array ( [firstname] => Peter [lastname] => Griffin [age] => 38 )
    使用没有对应变量名的字符串,以及一个变量名数组
    $firstname = "Peter";
    $lastname = "Griffin";
    $age = "38";
    $name = array("firstname", "lastname");
    $result = compact($name, "location", "age");
    print_r($result);// Array ( [firstname] => Peter [lastname] => Griffin [age] => 38 )
     
    count(array,mode)计算数组中的单元数目或对象中的属性个数
    mode
    0 - 默认。不检测多维数组(数组中的数组)
    1 - 检测多维数组
    注释:当变量未被设置,或是变量包含一个空的数组,该函数会返回 0。可使用 isset() 变量来测试变量是否被设置
    $people = array("Peter", "Joe", "Glenn", "Cleveland");
    $result = count($people);
    echo $result;// 4
     
    key(array)返回数组内部指针当前指向元素的键名
    $people = array("Peter", "Joe", "Glenn", "Cleveland");
    echo key($people);//0
     
    current(array)返回数组中的当前元素(单元)或者使用pos()
    注释:如果有空的元素,或元素没有值,该函数也返回 FALSE,该函数不会移动内部指针要做到这一点,请使用next()和prev()函数
    $people = array("Peter", "Joe", "Glenn", "Cleveland");
    echo current($people);// Peter
     
    each(array)生成一个由数组当前内部指针所指向的元素的键名和键值组成的数组,并把内部指针向前移动
    $people = array("Peter", "Joe", "Glenn", "Cleveland");
    print_r (each($people));// Array ( [1] => Peter [value] => Peter [0] => 0 [key] => 0 )
    each() 经常和 list() 结合使用来遍历数组。本例与上例类似,不过循环输出了整个数组
    $people = array("Peter", "Joe", "Glenn", "Cleveland");
    reset($people);
    while (list($key, $val) = each($people)){
      echo "$key => $val<br />";
    }//0 => Peter
    1 => Joe
    2 => Glenn
    3 => Cleveland
     
    end(array)将数组内部指针指向最后一个元素,并返回该元素的值(如果成功)
    $people = array("Peter", "Joe", "Glenn", "Cleveland");
    echo current($people) . " ". end($people);//Peter Cleveland
     
    extract(array,extract_rules,prefix)从数组中把变量导入到当前的符号表中
    $a = 'Original';
    $my_array = array("a" => "Cat","b" => "Dog", "c" => "Horse");
    extract($my_array);
    echo "\$a = $a; \$b = $b; \$c = $c";//$a = Cat; $b = Dog; $c = Horse
    使用全部参数
    $a = 'Original';
    $my_array = array("a" => "Cat","b" => "Dog", "c" => "Horse");
    extract($my_array, EXTR_PREFIX_SAME, 'dup');
    echo "\$a = $a; \$b = $b; \$c = $c; \$dup_a = $dup_a;";// $a = Original; $b = Dog; $c = Horse; $dup_a = Cat;
     
    in_array(value,array,type)在数组中搜索给定的值
    注释:如果 value 参数是字符串,且 type 参数设置为 true,则搜索区分大小写
    $people = array("Peter", "Joe", "Glenn", "Cleveland");
    if (in_array("Glenn",$people)){echo "Match found";}
    else{echo "Match not found";}//Match found
     
    $people = array("Peter", "Joe", "Glenn", "Cleveland", 23);
    if (in_array(23,$people, TRUE)){echo "Match found";}
    else{echo "Match not found";}// Match found
     
    if (in_array("23",$people, TRUE)){echo "Match found";}
    else{echo "Match not found";}//Match not found
     
    if (in_array("Glenn",$people, TRUE)){echo "Match found";}
    else{echo "Match not found";}// Match found
     
    krsort(array,sorttype)将数组按照键逆向排序,为数组值保留原来的键
    ksort(array,sorttype)按照键名对数组排序,为数组值保留原来的键
    $my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
    krsort($my_array);
    print_r($my_array);// Array ( [c] => Horse [b] => Cat [a] => Dog )
    ksort($my_array);
    print_r($my_array);// Array ( [a] => Dog [b] => Cat [c] => Horse )
     
    list(var1,var2...)用数组中的元素为一组变量赋值,与array()类似,list() 实际上是一种语言结构,不是函数
    $my_array = array("Dog","Cat","Horse");
    list($a, $b, $c) = $my_array;
    echo "I have several animals, a $a, a $b and a $c.";// I have several animals, a Dog, a Cat and a Horse.
    $my_array = array("Dog","Cat","Horse");
    list($a, , $c) = $my_array;
    echo "Here I only use the $a and $c variables.";// Here I only use the Dog and Horse variables.
     
    natcasesort(array)用不区分大小写的自然顺序算法对给定数组中的元素排序
    $temp_files = array("temp15.txt","Temp10.txt","temp1.txt","Temp22.txt","temp2.txt");
    natsort($temp_files);
    print_r($temp_files);// Array ( [1] => Temp10.txt [3] => Temp22.txt [2] => temp1.txt [4] => temp2.txt [0] => temp15.txt )
    natcasesort($temp_files);
    print_r($temp_files);// Array ( [2] => temp1.txt [4] => temp2.txt [1] => Temp10.txt [0] => temp15.txt [3] => Temp22.txt )
     
    next(array)把指向当前元素的指针移动到下一个元素的位置,并返回当前元素的值
    如果内部指针已经超过数组的最后一个元素,函数返回 false
    $people = array("Peter", "Joe", "Glenn", "Cleveland");
    echo current($people) . " ".next($people); //Peter Joe
     
    prev(array)把指向当前元素的指针移动到上一个元素的位置,并返回当前元素的值
    如果内部指针已经超过数组的第一个元素之前,函数返回 false
    注意:如果数组包含空的单元,或者单元的值是 0 则该函数碰到这些单元也返回 FALSE。要正确遍历可能含有空单元或者单元值为 0 的数组,请参见each()函数
    $people = array("Peter", "Joe", "Glenn", "Cleveland");
    echo next($people) . “ “.prev($people); //Joe Peter
     
    range(first,second,step)该函数创建一个数组,包含从 first 到 second (包含 first 和 second)之间的整数或字符。如果 second 比 first 小,则返回反序的数组
    $number = range(0,5);
    print_r ($number);// Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 )
    $number = range(0,50,10);
    print_r ($number);// Array ( [0] => 0 [1] => 10 [2] => 20 [3] => 30 [4] => 40 [5] => 50 )
    $letter = range("a","d");
    print_r ($letter);// Array ( [0] => a [1] => b [2] => c [3] => d )
     
    reset(array)把数组的内部指针指向第一个元素,并返回这个元素的值
    $people = array("Peter", "Joe", "Glenn", "Cleveland");
    echo next($people) . “ “ .reset($people);// Joe Peter
     
    rsort(array,sorttype)对数组的元素按照键值进行逆向排序,与arsort()的功能基本相同
    sort(array,sorttype)按升序对给定数组的值排序
    $my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
    rsort($my_array);
    print_r($my_array);// Array ( [0] => Horse [1] => Dog [2] => Cat )
     
    shuffle(array)把数组中的元素按随机顺序重新排列
    $my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
    shuffle($my_array);
    print_r($my_array);// Array ( [0] => Horse [1] => Cat [2] => Dog )
     
    sizeof(array,mode)计算数组中的单元数目或对象中的属性个数
    mode
    0 - 默认。不检测多维数组(数组中的数组)。
    1 - 检测多维数组。
    注释:当变量未被设置,或是变量包含一个空的数组,该函数会返回 0。可使用 isset() 变量来测试变量是否被设置
    $people = array("Peter", "Joe", "Glenn", "Cleveland");
    $result = sizeof($people);
    echo $result;// 4
     
    uasort(array,sorttype)使用用户自定义的比较函数对数组排序,并保持索引关联(不为元素分配新的键)。
    uksort(array,sorttype)使用用户自定义的比较函数按照键名对数组排序,并保持索引关系
    function my_sort($a, $b){
    if ($a == $b) return 0;
    return ($a > $b) ? -1 : 1;
    }
    $people = array("Swanson" => "Joe",
    "Griffin" => "Peter", "Quagmire" => "Glenn",
    "swanson" => "joe", "griffin" => "peter",
    "quagmire" => "glenn");
    uasort($people, "my_sort");
    print_r ($people);// Array ( [griffin] => peter [swanson] => joe [quagmire] => glenn [Griffin] => Peter [Swanson] => Joe [Quagmire] => Glenn )
    uksort($people, "my_sort");
    print_r ($people);// Array ( [swanson] => joe [quagmire] => glenn [griffin] => peter [Swanson] => Joe [Quagmire] => Glenn [Griffin] => Peter )
     
    usort(array,sorttype)使用用户自定义的函数对数组排序
    function my_sort($a, $b){
    if ($a == $b) return 0;
    return ($a > $b) ? -1 : 1;
    }
    $arr = array("Peter", "glenn","Cleveland","peter","cleveland", "Glenn");
    usort($arr, "my_sort");
    print_r ($arr);// Array ( [0] => peter [1] => glenn [2] => cleveland [3] => Peter [4] => Glenn [5] => Cleveland )
  •  

转载请注明:谷谷点程序 » PHP函数查询参考手册之Array篇