php数组相关的函数总结

[code]
function printRE($res,$is_exit=1){
echo ‘<pre>';
print_r($res);
echo ‘</pre>';
$is_exit==1 && exit();
}

//用“自然排序”算法对数组排序
//自然排序:a1 a2 a3 a11 a22 a33
//标准排序:a1 a11 a2 a22 a3 a33
$my_array = array(‘a2′,’a11′,’a1′,’a22′,’a33′,’a3′);
natsort($my_array);
printRE($my_array);

//把数组中的元素按随机顺序重新排序
$my_array = array("red","green","blue","yellow","purple");
shuffle($my_array);
printRE($my_array);

//创建包含指定范围单元的数组,第三个参数是步长
$number = range(0,10,2);
printRE($number);

//一种遍历数组的方式
$my_array = array("Dog","Cat","Horse");
while (list($k,$v)=each($my_array)){
echo "key={$k},value={$v}<br/>";
}
//把数组中的值赋值给变量
$my_array = array("Dog","Cat","Horse");
list($a, $b, $c) = $my_array;
printRE("\$a = $a; \$b = $b; \$c = $c");

$people = array("Bill", "Steve", "Mark", "David");
$res = current($people);//返回数组中的当前元素 与pos()一样
printRE($res,0);
$res = end($people);//返回数组中的最后一个元素
printRE($res,0);
$res = key($people);//取得当前键的位置
printRE($res,0);
$res = prev($people);//返回数组中的前一个元素
printRE($res,0);
$res = next($people);//返回下一个元素
printRE($res,0);
$res = reset($people);//重置到第一个元素
printRE($res,0);
$res = each($people);//返回当前元素的键名和键值,并将指针向前移动一个位置
printRE($res);

//返回数组中元素的数目,和sizeof()功能一致
$cars=array("Volvo","BMW",array("Toyota",’aaa’));
$res = count($cars);
$res1 = count($cars,1);
printRE($res,0);//递归计数
printRE($res1);

//将数组还原成变量
$my_array = array("a" => "aaa","b" => "ccc", "c" => "ccc");
extract($my_array);
printRE("\$a = $a; \$b = $b; \$c = $c");

//将多个变量组装成数组,与extract()函数相反
$a = ‘aaa';
$b = ‘bbb';
$c = ‘ccc';
$res = compact(‘a’,’b’,’c’);
printRE($res);

$age=array("Bill"=>"60","Steve"=>"56","Aark"=>"31");
sort($age);//值升序,键名被删除
printRE($age);
rsort($age);//值降序,键名被删除
printRE($age);

asort($age);//对关联数组按照值进行升序排序,并保持索引关系
printRE($age);
arsort($age);//对关联数组按照值进行降序排序,并保持索引关系
printRE($age);

ksort($age);//对数组按照键名升序,并保持索引关系
printRE($age);
krsort($age);//对数组按照键名降序,并保持索引关系
printRE($age);

//删除数组中的第一个元素
$a=array("a"=>"red","b"=>"green","c"=>"blue");
$res = array_shift($a);
printRE($res,0);
printRE($a);

//删除数组中的最后一个元素
$a=array("red","green","blue");
$res = array_pop($a);
printRE($res,0);
printRE($a);

//在数组开头插入一个或多个元素
$a=array("a"=>"red","b"=>"green");
array_unshift($a,"blue","blue2");
printRE($a);

//将一个或多个元素插入数组的末尾(入栈)。
$a=array("red","green");
array_push($a,"blue","yellow");
printRE($a);

//删除数组中的重复值
$a=array("c"=>"aed","b"=>"green","a"=>"aed");
$res = array_unique($a);
printRE($res);

//用回调函数过滤数组中的元素
function ta($var)
{
return $var==3?true:false;
}
$a1=array("a","b",2,3,4);
$res = array_filter($a1, "ta");
printRE($res);

//对数组中的每个元素应用用户自定义函数
function t3($value,$key)
{
echo "The key $key has the value $value<br>";
}
$a=array("a"=>"red","b"=>"green","c"=>"blue");
array_walk($a,"t3");exit;

//通过使用用户自定义函数,以字符串返回数组。
function t2($v1,$v2)
{
return $v1 . "-" . $v2;
}
$a=array("Dog","Cat","Horse");
$res = array_reduce($a,"t2",5);
printRE($res);

//把数组中的每个值发送到用户自定义函数,返回新的值。
function t1($v)
{
return($v*$v);
}

$a=array(1,2,3,4,5);
$res = array_map("t1",$a);
printRE($res);

//返回数组的所有值
$a=array("Name"=>"Bill","Age"=>"60","Country"=>"USA");
$res = array_values($a);
printRE($res);

//返回数组中所有值的和
$a=array(5,15,25);
$res = array_sum($a);
printRE($res);

//计算并返回数组的乘积
$a = array(5,2,3);
$res = array_product($a);
printRE($res);

//返回数组中被选定的元素
$a=array("red","green","blue","yellow","brown");
$res = array_slice($a,1,2,true);//从第二个开始选择两个 true – 保留键名
printRE($res);

//检查数组中是否存在指定的值
$people = array("Bill", "Steve", "Mark", "David");
if (in_array("Mark", $people))
{
echo "匹配已找到";
}
else
{
echo "匹配未找到";
}

//搜索数组中给定的值并返回键名,第三个参数如果被设置为 TRUE,则函数在数组中搜索数据类型和值都一致的元素
$a=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"red",);
$res = array_search("red",$a);
printRE($res);

//以相反的顺序返回数组,倒序
$a=array("a"=>"Volvo","b"=>"BMW","c"=>"Toyota");
$a=array("Volvo","c"=>"BMW","Toyota");
$res = array_reverse($a,true);
printRE($res);

//递归地使用后面数组的值替换第一个数组的值。直白点就是可以处理二维数组
$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"));
$res = array_replace_recursive($a1,$a2,$a3);
printRE($res);

//使用后面数组的值替换第一个数组的值。
//键在$a1中存在$a2中不存在,则保留$a1中的值;$a1中不存在,$a2中存在,则$a2覆盖$a1;都存在时直接替换
$a1=array("a"=>"red","b"=>"green");
$a2=array("a"=>"orange","burgundy");
$res = array_replace($a1,$a2);
printRE($res);

$a1=array("red","green");
$a2=array("blue","yellow");
$res = array_replace($a1,$a2);
printRE($res);

//返回数组中一个或多个随机的键
$a=array("red","green","blue","yellow","brown");
$random_keys=array_rand($a,3);
printRE($random_keys);

//用值将数组填补到指定长度,负数表示从前面进行填充
$a=array("red","green");
$res = array_pad($a,5,"blue");
printRE($res);

//使用指定的键和值填充数组
$keys=array("a","b","c","d");
$res=array_fill_keys($keys,"blue");
printRE($res);

//用给定的键值填充数组。从第三个位置开始用blue填充4位
$res=array_fill(3,4,"blue");
printRE($res);

//对多个数组或多维数组进行排序(字符串键名将被保留,但是数字键名将被重新索引;可以在每个数组后设置排序顺序和排序类型参数。如果没有设置,每个数组参数会使用默认值。)
//3、对年龄降序,用户名升序
$array = array(
array("id"=>1,"age"=>20,"username"=>"user3"),
array("id"=>2,"age"=>21,"username"=>"user2"),
array("id"=>3,"age"=>20,"username"=>"user1"),
array("id"=>4,"age"=>22,"username"=>"user4")
);

foreach ($array as $key=>$value){
$age[$key] = $value[‘age’];
$name[$key] = $value[‘username’];
}

array_multisort($age,SORT_NUMERIC,SORT_DESC,$name,SORT_STRING,SORT_ASC,$array);
printRE($array);

//2、对多个数组排序,先对第一个数组进行排序,接着是其他数组,如果两个或多个值相同,它将对下一个数组进行排序
//注意:对这个函数的正确理解是,先对第一个数组进行排序,然后对应移动第二个数组的顺序,如果第一个数组中有相同的值,这个时候才会用第二个数组来排序
$a1=array("Dog","Dog","Cat");
$a2=array("Pluto","Fido","Missy");
array_multisort($a1,$a2);
printRE($a1,0);
printRE($a2);

//1、默认SORT_ASC,可使用SORT_DESC
$a=array("Dog","Cat","Horse","Bear","Zebra");
array_multisort($a,SORT_DESC);
printRE($a);

//递归地合并一个或多个数组
$a1=array("a"=>"red","b"=>"green");
$a2=array("c"=>"blue","b"=>"yellow");
$res = array_merge_recursive($a1,$a2);
printRE($res);

//把一个或多个数组合并为一个数组,如果键名相同,后面会覆盖前面的
$a1=array("a"=>"red","b"=>"green");
$a2=array("c"=>"blue","b"=>"yellow");
$res = array_merge($a1,$a2);
printRE($res);

//返回包含数组中所有键名的一个新数组
$a=array("Volvo"=>"XC90","BMW"=>"X5","Toyota"=>"Highlander");
$res = array_keys($a);
printRE($res);

//检查指定的键名是否存在于数组中
$a=array("Volvo"=>"XC90","BMW"=>"X5");
if (array_key_exists("Volvo",$a))
{
die("键存在!");
}
else
{
die("键不存在!");
}

//比较两个数组的值,并返回交集
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
$res=array_intersect($a1,$a2);
printRE($res);

//反转数组中所有的键以及它们关联的值
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$res=array_flip($a1);
printRE($res);

//比较数组,返回差集(比较键名和键值,使用用户自定义的键名比较函数)。
function myfunction($a,$b)
{
echo $a.’ ‘.$b.'<br/>';
return 1;
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"1","b"=>"2","c"=>"3");
$a2=array("a"=>"1","b"=>"2","c"=>"3");
$res=array_diff_uassoc($a1,$a2,"app\web\controllers\myfunction");
printRE($res);

//取数组的差集(键比较取差值),在$a1不在$a2\$a3的值
$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");
$res=array_diff_key($a1,$a2,$a3);
printRE($res);

//取数组的差集(键值同时比较取差值),在$a1不在$a2\$a3的值
$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");
$res=array_diff_assoc($a1,$a2,$a3);
printRE($res);

//取数组的差集(只用值取差值),在$a1不在$a2\$a3的值
$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");
$res=array_diff($a1,$a2,$a3);
printRE($res);

//统计数组中所有的值出现的次数
$a=array("A","Cat","Dog","A","Dog");
$res = array_count_values($a);
printRE($res);

//通过合并两个数组来创建一个新数组,其中的一个数组元素为键名,另一个数组元素为键值
$fname = array("Peter","Ben","Joe");//key
$age = array("35","37","43");//value
$res = array_combine($fname,$age);
printRE($res);

//返回数组单列的值,php5.5+
$a = array(
array(
‘id’ => 5698,
‘first_name’ => ‘Peter’,
‘last_name’ => ‘Griffin’,
),
array(
‘id’ => 4767,
‘first_name’ => ‘Ben’,
‘last_name’ => ‘Smith’,
),
array(
‘id’ => 3809,
‘first_name’ => ‘Joe’,
‘last_name’ => ‘Doe’,
)
);

$res = array_column($a, ‘last_name’);//提取last_name列
$res = array_column($a, ‘last_name’, ‘id’);//用id作为键
printRE($res);

//把一个数组分割为新的数组块,一维数组分割后就变成了二维数组
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43","Harry"=>"50");
$res = array_chunk($age,2,true);//保留原键名
$res = array_chunk($age,2,false);//键名从0重新开始
printRE($res);

//将数组的键名返回为大写或者小写
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
$res = array_change_key_case($age,CASE_UPPER);
$res = array_change_key_case($age,CASE_LOWER);
printRE($res);
[/code]