PHP手册Array数组大全(解析)

Posted 贵哥的编程之路

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP手册Array数组大全(解析)相关的知识,希望对你有一定的参考价值。

PHP手册Array数组大全(解析)_二维数组


//array_change_key_case() 
$age=[cyg=>"kkk","liwen"=>"70"];
print_r(array_change_key_case($age,CASE_UPPER));//把键名转换成大写,默认是小写

PHP手册Array数组大全(解析)_数组_02


//array_chunk() 
$cars=array("Volvo","BMW","Toyota","Honda","Mercedes","Opel");
print_r(array_chunk($cars,2));//两个元素(2)组成一个数组.形成了一个二维数组啦

PHP手册Array数组大全(解析)_二维数组_03


//array_chunk() 
$cars=array("Volvo","BMW","Toyota","Honda","Mercedes","Opel");
print_r(array_chunk($cars,2,true));//两个元素组成一个数组,建名从0开始,递增

PHP手册Array数组大全(解析)_二维数组_04


//array_column() 
// 表示由数据库返回的可能记录集的数组
$a = array(
array(
id => 5698,
first_name => Bill,
last_name => Gates,
),
array(
id => 4767,
first_name => Steve,
last_name => Jobs,
),
array(
id => 3809,
first_name => Mark,
last_name => Zuckerberg,
)
);
$last_names=array_column($a,last_name);//取出健名是last_name的值
print_r($last_names);

PHP手册Array数组大全(解析)_键值_05


//array_column() 
// 表示由数据库返回的可能记录集的数组
$a = array(
array(
id => 5698,
first_name => Bill,
last_name => Gates,
),
array(
id => 4767,
first_name => Steve,
last_name => Jobs,
),
array(
id => 3809,
first_name => Mark,
last_name => Zuckerberg,
)
);
$last_names=array_column($a,last_name,id);//取出last_name的值,以该二维数组的id为其对应的下标
print_r($last_names);

PHP手册Array数组大全(解析)_数组_06

//array_combine()

$fname=array("Bill","Steve","Mark");
$age=array("60","56","31");
$c=array_combine($fname, $age);
print_r($c);//$fname作为下标,$gae作为值

PHP手册Array数组大全(解析)_键值_07


$a=array("A","Pat","Dog","d","Dog");
print_r(array_count_values($a));
//每个元素看后面有没有一样的,有就+1
//默认为1
?>

PHP手册Array数组大全(解析)_键值_08


$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");

$result=array_diff($a1,$a2);
print_r($result);//返回两个数组中都没有的值
?>

PHP手册Array数组大全(解析)_二维数组_09


$a1=array("a"=>"red","b"=>"green66","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"red","b"=>"green","c"=>"blue");
$result=array_diff_assoc($a1,$a2);
print_r($result);//比较两个数组里面的下标+值,如果不一样,就输出.

PHP手册Array数组大全(解析)_键值_10


$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"red","f"=>"green","g"=>"blue");
$a3=array("h"=>"red","b"=>"green","g"=>"blue");

$result=array_diff_assoc($a1,$a2,$a3);
print_r($result);//数组作比较,下标c和下标d都没有与其他数组的下标+值不一样,输出
?>

PHP手册Array数组大全(解析)_二维数组_11


$a1=array("a"=>"red","b"=>"green66","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"red","b"=>"green","c"=>"blue");
$result=array_diff_key($a1,$a2);
print_r($result);//只比较建名

PHP手册Array数组大全(解析)_键值_12


$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("c"=>"yellow","d"=>"black","e"=>"brown");
$a3=array("f"=>"green","c"=>"purple","g"=>"red");

$result=array_diff_key($a1,$a2,$a3);
print_r($result);//比较健名。下标a没有对应的下标a,下标b没有对应的下标b,下标c有可以.
?>

PHP手册Array数组大全(解析)_二维数组_13


function myfunction($a,$b)

if ($a===$b)

return 0;//如果意义,返回0

return ($a>$b)?1:-1;//不相等,1或者-1的时候都会输出出来.

$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("d"=>"red","b"=>"green","e"=>"blue");
$result=array_diff_uassoc($a1,$a2,myfunction);
print_r($result);//以第一个数组对比其他数组。

PHP手册Array数组大全(解析)_键值_14


function myfunction($a,$b)

if ($a===$b)

return 0;

return ($a>$b)?1:-1;


$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"red","b"=>"green","d"=>"blue");
$a3=array("e"=>"yellow","a"=>"red","d"=>"blue");

$result=array_diff_uassoc($a1,$a2,$a3,"myfunction");
print_r($result);//以第一个数组与其他数组作比较,c的下标+值其他数组没有一模一样的,就输出出来啦
?>

PHP手册Array数组大全(解析)_键值_15


function myfunction($a,$b)

if ($a===$b)

return 0;

return ($a>$b)?1:-1;


$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"blue","b"=>"black","e"=>"blue");

$result=array_diff_ukey($a1,$a2,"myfunction");
print_r($result);//以第一个数组的为例子,第一个数组的那个下标在其他数组中没有一样的话,就输出出来
?>

PHP手册Array数组大全(解析)_键值_16


$a1=array_fill(3,4,"blue");//下标都为blue
print_r($a1);//从下标3开始,输出四个值。+1的值

PHP手册Array数组大全(解析)_键值_17


$keys=array("a","b","c","d");
$a1=array_fill_keys($keys,"blue");
print_r($a1);//a b c d作为blue的下标,下标多少个输出多少个元素

PHP手册Array数组大全(解析)_二维数组_18


$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$result=array_flip($a1);
print_r($result);//交换数组中的某一个元素的jian值+jian名。比如a=>b变成了b=>a

PHP手册Array数组大全(解析)_二维数组_19


$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red666","f"=>"green","g"=>"blue");

$result=array_intersect($a1,$a2);
print_r($result);//比较数组$a1与数组$a2相同的建值.如果相同的就输出出来(交集)
?>

PHP手册Array数组大全(解析)_键值_20


$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"black","g"=>"purple");
$a3=array("a"=>"red","b"=>"black","h"=>"yellow");

$result=array_intersect($a1,$a2,$a3);
print_r($result);//以$a1为例,red相同的就输出出来a=>red(交集),因为是三个数组,所以必须三个相同的建名或者建值才行
?>

PHP手册Array数组大全(解析)_数组_21


$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("6"=>"red","b"=>"green","c"=>"blue");
$result=array_intersect_assoc($a1,$a2);
print_r($result);//以$a1为基准,建名+建值一样才能输出出来

PHP手册Array数组大全(解析)_键值_22


$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"red","b"=>"green","g"=>"blue");
$a3=array("a"=>"red","b"=>"green","g"=>"blue");

$result=array_intersect_assoc($a1,$a2,$a3);
print_r($result);//因为是三个数组作比较,所以三个一样的建名+建值一样才能输出出来
?>

PHP手册Array数组大全(解析)_二维数组_23


$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"red","c"=>"blu66","d"=>"pink");

$result=array_intersect_key($a1,$a2);
print_r($result);//以$a1为基准,建名相同的就输出出来
?>

PHP手册Array数组大全(解析)_键值_24


$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("c"=>"yellow","d"=>"black","e"=>"brown");
$a3=array("f"=>"green","c"=>"purple","g"=>"red");

$result=array_intersect_key($a1,$a2,$a3);
print_r($result);//以$a1为基准,三个一样的建名才能输出
?>

PHP手册Array数组大全(解析)_数组_25


$a=array("Volvo"=>"XC90","BMW"=>"X5");
echo array_key_exists("Volvo6",$a)?"存在":"不存在";
//判断数组中是否有这个健民

PHP手册Array数组大全(解析)_键值_26


$a=array("Volvo"=>"XC90","BMW"=>"X5","Toyota"=>"Highlander");
print_r(array_keys($a));//返回数组中的所有健名
?>

PHP手册Array数组大全(解析)_键值_27


$a=array("Volvo"=>"XC90","BMW"=>"X5","Toyota"=>"Highlander");
print_r(array_keys($a,"Highlander"));//取出健值所对应的建名
?>

PHP手册Array数组大全(解析)_键值_28


$a=array(10,20,30,"10");
print_r(array_keys($a,"10",true));
?>

PHP手册Array数组大全(解析)_键值_29


$a=array(10,20,30,66,"10");
print_r(array_keys($a,"10",false));//返回开始到"10"的范围
?>

PHP手册Array数组大全(解析)_数组_30


function mm($v)

return ($v*$v);

$a=array(1,2,3,4,5);
print_r(array_map("mm",$a));
//通过mm函数,返回新的数组

PHP手册Array数组大全(解析)_数组_31


$a1=array("Dog","Cat","cyg");
$a2=array("Puppy","Kitten","liwen");
print_r(array_map(null,$a1,$a2));
//两个数组通过array_map函数返回二维数组,第一个二维数组$a1[0],$a2[0].第一个二维数组$a1[1],$a2[1].以此类推

PHP手册Array数组大全(解析)_二维数组_32


$a1=array("Dog","Cat","cyg");
$a2=array("Puppy","Kitten","liwen");
print_r(array_merge($a1,$a2));//把两个数组合并为一个数组。下标是从0开始,随着·元素的多少而递增

PHP手册Array数组大全(解析)_数组_33


$a1=array("a"=>"red","b"=>"green");
$a2=array("c"=>"blue","b"=>"yellow");
print_r(array_merge($a1,$a2));//相同的下标都会后面的覆盖。合并两个数组

PHP手册Array数组大全(解析)_数组_34


$a=array(3=>"red",4=>"green");
print_r(array_merge($a));//合并下标是从零开始的
?>

PHP手册Array数组大全(解析)_二维数组_35


$a1=array("a"=>"red","b"=>"green");
$a2=array("c"=>"blue","b"=>"yellow");
print_r(array_merge_recursive($a1,$a2));//这个函数合并的如果有一样的下标会形成二维数组哦
?>

PHP手册Array数组大全(解析)_二维数组_36


$a=array("Dog","Cat","Horse","Bear","Zebra");
//$b=array_multisort($a);//返回1
array_multisort($a);
print_r($a);//开首字母进行排序

PHP手册Array数组大全(解析)_数组_37


$a1=array("Dog","Dog","Cat");
$a2=array("Pluto","Fido","Missy");
array_multisort($a1,SORT_ASC,$a2,SORT_DESC);
print_r($a1);//$a1进行升序排序,
print_r($a2);//$a2进行降序排序

PHP手册Array数组大全(解析)_二维数组_38


$a1=array(1,30,15,7,25);
$a2=array(4,30,20,41,66);
$num=array_merge($a1,$a2);
array_multisort($num,SORT_DESC,SORT_NUMERIC);
print_r($num);//合并两个数组,按数字进行降序排序

PHP手册Array数组大全(解析)_数组_39


$a=array("red","green");
print_r(array_pad($a,5,"blue"));//五个元素,不足的按照blue来填充

PHP手册Array数组大全(解析)_键值_40


$a=array("red","green");
print_r(array_pad($a,-5,"blue"));//五个元素,不足的按照blue来填充,-代表填充前面

PHP手册Array数组大全(解析)_数组_41


$a=array("red","green","blue");
array_pop($a);
print_r($a);//删除最后一个元素
?>

PHP手册Array数组大全(解析)_键值_42


$a=array(5,5);
echo(array_product($a));//计算数组的乘积
?>

PHP手册Array数组大全(解析)_键值_43


$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);//往数组的尾部插入blue+yellow
?>

PHP手册Array数组大全(解析)_二维数组_44


$a=array("red","green","blue","yellow","brown");//你刷新几次结果都会不一样哦
$b=array_rand($a,3);
echo $a[$b[0]]."";
echo $a[$b[1]]."";
echo $a[$b[2]]."";
//返回三个数组元素,刷新效果会不一样

PHP手册Array数组大全(解析)_键值_45


$a=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");//刷新几次效果都会不一样
print_r(array_rand($a,1));//返回下标.每次刷新只返回一个元素的下标

PHP手册Array数组大全(解析)_二维数组_46


$a=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
print_r(array_rand($a,2));//返回两个元素.下标是依次递增,从0开始

PHP手册Array数组大全(解析)_数组_47


function myfunction($v1,$v2)

return $v1 . "-" . $v2;

$a=array("Dog","Cat","Horse");
print_r(array_reduce($a, "myfunction",15));
//返回把数组转换成字符串

PHP手册Array数组大全(解析)_数组_48


$a1=array("red","green","blue","yellow");
$a2=array(0=>"orange",3=>"burgundy");
print_r(array_replace($a1,$a2));//后面的覆盖前面的
?>

PHP手册Array数组大全(解析)_键值_49


$a1=array("a"=>array("red"),"b"=>array("green","blue"),);
$a2=array("a"=>array("yellow"),"b"=>array("black"));
print_r(array_replace_recursive($a1,$a2));//相同关联下标的,会转换成二维数组,二维数组中,举个例子,yellow替换掉red。black替换掉green。效果如下

PHP手册Array数组大全(解析)_键值_50


$a1=array("a"=>array("red"),"b"=>array("green","blue"));
$a2=array("a"=>array("yellow"),"b"=>array("black"));
$a3=array("a"=>array("orange"),"b"=>array("burgundy"));
print_r(array_replace_recursive($a1,$a2,$a3));//三个数组关联数组会转换成二维数组,相同下标的后面会覆盖前面的,比如orange覆盖red yellow
?>

PHP手册Array数组大全(解析)_二维数组_51


$a=array("a"=>"Volvo","b"=>"BMW","c"=>"Toyota");
print_r(array_reverse($a));//倒序输出关联数组

PHP手册Array数组大全(解析)_数组_52


$a=array("Volvo","XC90",array("BMW","Toyota"));
$preserve=array_reverse($a,true);
print_r($preserve);//下标从高到底排序。
?>

PHP手册Array数组大全(解析)_数组_53


$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_search("red",$a);//元素值的下标是啥?

PHP手册Array数组大全(解析)_数组_54


$a=array("a"=>"5","b"=>5,"c"=>"5");
echo array_search(5,$a,true);//健值的下标是啥?

PHP手册Array数组大全(解析)_二维数组_55


$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_shift($a);//删除掉第一个元素
print_r($a);

PHP手册Array数组大全(解析)_二维数组_56


$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,2));//从第一个元素开始,删除两个元素

PHP手册Array数组大全(解析)_键值_57


$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,1,2));//输出$a[1]+$a[2]这两个元素

PHP手册Array数组大全(解析)_二维数组_58


$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,-2,1));//-2代表从后面往前数。-1==brown。yellow代表-2。1代表取出一个元素

PHP手册Array数组大全(解析)_数组_59


$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,1,2));//从$a[1]开始取出两个元素

PHP手册Array数组大全(解析)_键值_60


$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,2,2,true));//从$a[2]开始取出两个元素,true代表在原数组中是什么下标输出就是什么下标

PHP手册Array数组大全(解析)_二维数组_61


$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"purple","b"=>"orange");
array_splice($a1,0,3,$a2);//从0开始,删除三个元素
print_r($a1);

PHP手册Array数组大全(解析)_键值_62


$a1=array("0"=>"red","1"=>"green");
$a2=array("0"=>"purple","1"=>"orange");
array_splice($a1,1,0,$a2);//从1开始,删除0个元素.代表合并
print_r($a1);
?>

PHP手册Array数组大全(解析)_数组_63


$a=array("a"=>52.2,"b"=>13.7,"c"=>0.9);
echo array_sum($a);//元素值相加

PHP手册Array数组大全(解析)_键值_64


function myfunction($a,$b)

if ($a===$b)

return 0;//0代表相等

return ($a>$b)?1:-1;//1或者-1都是输出的值


$a1=array("a"=>"black","b"=>"green","c"=>"blue");
$a2=array("a"=>"blue","b"=>"black","e"=>"blue");
$result=array_udiff($a1,$a2,"myfunction");//只比较键值。两

以上是关于PHP手册Array数组大全(解析)的主要内容,如果未能解决你的问题,请参考以下文章

php 数组排序

PHP 数组函数大全

PHP数组大全

PHP数组函数详解大全

php函数 PHP程序开发参考手册目录

php数组常用函数