php参考手册Array函数(完结了)

2022-05-06 11:42:49 浏览数 (2)

代码语言:javascript复制
<?php
//array_change_key_case() 
$age=['cyg'=>"kkk","liwen"=>"70"];
print_r(array_change_key_case($age,CASE_UPPER));//把键名转换成大写,默认是小写
代码语言:javascript复制
<?php
//array_chunk() 
$cars=array("Volvo","BMW","Toyota","Honda","Mercedes","Opel");
print_r(array_chunk($cars,2));//两个元素(2)组成一个数组.形成了一个二维数组啦
代码语言:javascript复制
<?php
//array_chunk() 
$cars=array("Volvo","BMW","Toyota","Honda","Mercedes","Opel");
print_r(array_chunk($cars,2,true));//两个元素组成一个数组,建名从0开始,递增
代码语言:javascript复制
<?php
//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);
代码语言:javascript复制
<?php
//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);

//array_combine()

代码语言:javascript复制
<?php
$fname=array("Bill","Steve","Mark");
$age=array("60","56","31");
$c=array_combine($fname, $age);
print_r($c);//$fname作为下标,$gae作为值
代码语言:javascript复制
<?php
$a=array("A","Pat","Dog","d","Dog");
print_r(array_count_values($a));
//每个元素看后面有没有一样的,有就 1
//默认为1
?>
代码语言:javascript复制
<?php
$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);//返回两个数组中都没有的值
?>
代码语言:javascript复制
<?php
$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);//比较两个数组里面的下标 值,如果不一样,就输出.
代码语言:javascript复制
<?php
$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都没有与其他数组的下标 值不一样,输出
?>
代码语言:javascript复制
<?php
$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);//只比较建名
代码语言:javascript复制
<?php
$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有可以.
?>
代码语言:javascript复制
<?php
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);//以第一个数组对比其他数组。
代码语言:javascript复制
<?php
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的下标 值其他数组没有一模一样的,就输出出来啦
?>
代码语言:javascript复制
<?php
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);//以第一个数组的为例子,第一个数组的那个下标在其他数组中没有一样的话,就输出出来
?>
代码语言:javascript复制
<?php
$a1=array_fill(3,4,"blue");//下标都为blue
print_r($a1);//从下标3开始,输出四个值。 1的值
代码语言:javascript复制
<?php
$keys=array("a","b","c","d");
$a1=array_fill_keys($keys,"blue");
print_r($a1);//a b c d作为blue的下标,下标多少个输出多少个元素
代码语言:javascript复制
<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$result=array_flip($a1);
print_r($result);//交换数组中的某一个元素的jian值 jian名。比如a=>b变成了b=>a
代码语言:javascript复制
<?php
$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相同的建值.如果相同的就输出出来(交集)
?>
代码语言:javascript复制
<?php
$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(交集),因为是三个数组,所以必须三个相同的建名或者建值才行
?>
代码语言:javascript复制
<?php
$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为基准,建名 建值一样才能输出出来
代码语言:javascript复制
<?php
$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);//因为是三个数组作比较,所以三个一样的建名 建值一样才能输出出来
?>
代码语言:javascript复制
<?php
$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为基准,建名相同的就输出出来
?>
代码语言:javascript复制
<?php
$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为基准,三个一样的建名才能输出
?>
代码语言:javascript复制
<?php
$a=array("Volvo"=>"XC90","BMW"=>"X5");
echo array_key_exists("Volvo6",$a)?"存在":"不存在";
//判断数组中是否有这个健民
代码语言:javascript复制
<?php
$a=array("Volvo"=>"XC90","BMW"=>"X5","Toyota"=>"Highlander");
print_r(array_keys($a));//返回数组中的所有健名
?>
代码语言:javascript复制
<?php
$a=array("Volvo"=>"XC90","BMW"=>"X5","Toyota"=>"Highlander");
print_r(array_keys($a,"Highlander"));//取出健值所对应的建名
?>
代码语言:javascript复制
<?php
$a=array(10,20,30,"10");
print_r(array_keys($a,"10",true));
?>
代码语言:javascript复制
<?php
$a=array(10,20,30,66,"10");
print_r(array_keys($a,"10",false));//返回开始到"10"的范围
?>
代码语言:javascript复制
<?php
function mm($v)
{
	return ($v*$v);
}
$a=array(1,2,3,4,5);
print_r(array_map("mm",$a));
//通过mm函数,返回新的数组
代码语言:javascript复制
<?php
$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].以此类推
代码语言:javascript复制
<?php
$a1=array("Dog","Cat","cyg");
$a2=array("Puppy","Kitten","liwen");
print_r(array_merge($a1,$a2));//把两个数组合并为一个数组。下标是从0开始,随着·元素的多少而递增
代码语言:javascript复制
<?php
$a1=array("a"=>"red","b"=>"green");
$a2=array("c"=>"blue","b"=>"yellow");
print_r(array_merge($a1,$a2));//相同的下标都会后面的覆盖。合并两个数组
代码语言:javascript复制
<?php
$a=array(3=>"red",4=>"green");
print_r(array_merge($a));//合并下标是从零开始的
?>
代码语言:javascript复制
<?php
$a1=array("a"=>"red","b"=>"green");
$a2=array("c"=>"blue","b"=>"yellow");
print_r(array_merge_recursive($a1,$a2));//这个函数合并的如果有一样的下标会形成二维数组哦
?>
代码语言:javascript复制
<?php
$a=array("Dog","Cat","Horse","Bear","Zebra");
//$b=array_multisort($a);//返回1
array_multisort($a);
print_r($a);//开首字母进行排序
代码语言:javascript复制
<?php
$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进行降序排序
代码语言:javascript复制
<?php
$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);//合并两个数组,按数字进行降序排序
代码语言:javascript复制
<?php
$a=array("red","green");
print_r(array_pad($a,5,"blue"));//五个元素,不足的按照blue来填充
代码语言:javascript复制
<?php
$a=array("red","green");
print_r(array_pad($a,-5,"blue"));//五个元素,不足的按照blue来填充,-代表填充前面
代码语言:javascript复制
<?php
$a=array("red","green","blue");
array_pop($a);
print_r($a);//删除最后一个元素
?>
代码语言:javascript复制
<?php
$a=array(5,5);
echo(array_product($a));//计算数组的乘积
?>
代码语言:javascript复制
<?php
$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);//往数组的尾部插入blue yellow
?>
代码语言:javascript复制
<?php
$a=array("red","green","blue","yellow","brown");//你刷新几次结果都会不一样哦
$b=array_rand($a,3);
echo $a[$b[0]]."<br />";
echo $a[$b[1]]."<br />";
echo $a[$b[2]]."<br />";
//返回三个数组元素,刷新效果会不一样
代码语言:javascript复制
<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");//刷新几次效果都会不一样
print_r(array_rand($a,1));//返回下标.每次刷新只返回一个元素的下标
代码语言:javascript复制
<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
print_r(array_rand($a,2));//返回两个元素.下标是依次递增,从0开始
代码语言:javascript复制
<?php
function myfunction($v1,$v2)
{
return $v1 . "-" . $v2;
}
$a=array("Dog","Cat","Horse");
print_r(array_reduce($a, "myfunction",15));
//返回把数组转换成字符串
代码语言:javascript复制
<?php
$a1=array("red","green","blue","yellow");
$a2=array(0=>"orange",3=>"burgundy");
print_r(array_replace($a1,$a2));//后面的覆盖前面的
?>
代码语言:javascript复制
<?php
$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。效果如下
代码语言:javascript复制
<?php
$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
?>
代码语言:javascript复制
<?php
$a=array("a"=>"Volvo","b"=>"BMW","c"=>"Toyota");
print_r(array_reverse($a));//倒序输出关联数组
代码语言:javascript复制
<?php
$a=array("Volvo","XC90",array("BMW","Toyota"));
$preserve=array_reverse($a,true);
print_r($preserve);//下标从高到底排序。
?>
代码语言:javascript复制
<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_search("red",$a);//元素值的下标是啥?
代码语言:javascript复制
<?php
$a=array("a"=>"5","b"=>5,"c"=>"5");
echo array_search(5,$a,true);//健值的下标是啥?
代码语言:javascript复制
<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_shift($a);//删除掉第一个元素
print_r($a);
代码语言:javascript复制
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,2));//从第一个元素开始,删除两个元素
代码语言:javascript复制
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,1,2));//输出$a[1] $a[2]这两个元素
代码语言:javascript复制
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,-2,1));//-2代表从后面往前数。-1==brown。yellow代表-2。1代表取出一个元素
代码语言:javascript复制
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,1,2));//从$a[1]开始取出两个元素
代码语言:javascript复制
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,2,2,true));//从$a[2]开始取出两个元素,true代表在原数组中是什么下标输出就是什么下标
代码语言:javascript复制
<?php
$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);
代码语言:javascript复制
<?php
$a1=array("0"=>"red","1"=>"green");
$a2=array("0"=>"purple","1"=>"orange");
array_splice($a1,1,0,$a2);//从1开始,删除0个元素.代表合并
print_r($a1);
?>
代码语言:javascript复制
<?php
$a=array("a"=>52.2,"b"=>13.7,"c"=>0.9);
echo array_sum($a);//元素值相加
代码语言:javascript复制
<?php
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");//只比较键值。两个数组,值一样不输出,独一无二的值就输出。(差值)
print_r($result);
代码语言:javascript复制
<?php
function myfunction($a,$b)
{
if ($a===$b)
  {
  return 0;//相等就都不输出
  }
  return ($a>$b)?1:-1;//不相等的都输出(差值)
}

$a1=array("a"=>"red","b"=>"green","c"=>"blue","yellow");
$a2=array("A"=>"red","b"=>"GREEN","yellow","black");
$a3=array("a"=>"green","b"=>"red","yellow","black");

$result=array_udiff($a1,$a2,$a3,"myfunction");
print_r($result);//三个数组,大小写不区分。//只比较键值。三个数组,值一样不输出,举个例子,$a1 a2 $a3都有green就不输出。只有$a2 $a3有black就输出.
?>
代码语言:javascript复制
<?php
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"=>"blue","c"=>"green");
$result=array_udiff_assoc($a1,$a2,"myfunction");
print_r($result);//比较键值 键名都一样就输出。一样就输出
代码语言:javascript复制
<?php
function myfunction_key($a,$b)
{
if ($a===$b)
  {
  return 0;
  }
  return ($a>$b)?1:-1;
}

function myfunction_value($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","c"=>"green");
$result=array_udiff_uassoc($a1,$a2,"myfunction_key","myfunction_value");
print_r($result);//比较两个数组的键名和键值.第一个函数比较键名,第二个函数比较键值!
代码语言:javascript复制
<?php
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"=>"green","b"=>"black","e"=>"blue");

$result=array_uintersect($a1,$a2,'myfunction');
print_r($result);//只比较键值。交集的这种,键值一样就输出
代码语言:javascript复制
<?php
function myfunction($a,$b)
{
if ($a===$b)
  {
  return 0;
  }
  return ($a>$b)?1:-1;
}

$a1=array("a"=>"red","b"=>"green","c"=>"blue","yellow");
$a2=array("A"=>"red","b"=>"GREEN","yellow","black");
$a3=array("a"=>"green","b"=>"red","yellow","black");

$result=array_uintersect($a1,$a2,$a3,"myfunction");
print_r($result);//返回交集,什么叫做交集,就是每个数组都有的叫做交集。只比较键值
?>
代码语言:javascript复制
<?php
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"=>"blue","c"=>"green");

$result=array_uintersect_assoc($a1,$a2,"myfunction");
print_r($result);//比较键值 键名。每个数组都有的叫做交集
?>
代码语言:javascript复制
<?php
function myfunction_key($a,$b)
{
if ($a===$b)
  {
  return 0;
  }
  return ($a>$b)?1:-1;
}

function myfunction_value($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","c"=>"green");

$result=array_uintersect_uassoc($a1,$a2,"myfunction_key","myfunction_value");
print_r($result);//第一个函数比较名,第二个比较值.比较每个数组都有的元素就输出。比较交集
?>
代码语言:javascript复制
<?php
$a=array("a"=>"red","b"=>"green","c"=>"red");
print_r(array_unique($a));//如果有重复的元素,只保留第一个
代码语言:javascript复制
<?php
$a=array("a"=>"red","b"=>"green");
array_unshift($a,"blue");
print_r($a);//不需要赋值。blue放在开头。
代码语言:javascript复制
<?php
$a=array(0=>"red",1=>"green");
array_unshift($a,"blue");
print_r($a);//插入到开头,其他的下标后移一位
?>
代码语言:javascript复制
<?php
$a=array("Name"=>"Bill","Age"=>"60","Country"=>"USA");
print_r(array_values($a));//返回数组所有的值。下标是数字
代码语言:javascript复制
<?php
function myfunction($value,$key)
{
echo "The key $key has the value $value<br>";
}
$a=array("a"=>"red","b"=>"green","c"=>"blue");
array_walk($a,"myfunction");//对数组中每一个元素都引用到函数中的变量中
代码语言:javascript复制
<?php
function myfunction(&$value,$key)
{

 $value="yellow";
}
$a=array("a"=>"red","b"=>"green","c"=>"blue");
array_walk($a,"myfunction");
print_r($a);//更改数组元素的<<<值>>>
?>
代码语言:javascript复制
<?php
function myfunction($value,$key)
{
echo "键 $key 的值是 $value 。<br>";
}
$a1=array("a"=>"red","b"=>"green");
$a2=array($a1,"1"=>"blue","2"=>"yellow");
array_walk_recursive($a2,"myfunction");
//优点:可以操作二维三维数组
代码语言:javascript复制
<?php
$age=array("Bill"=>"0","Steve"=>"56","Mark"=>"31");
arsort($age);
foreach ($age as $key => $value) {
	# code...
	echo $key."<br />  ";
	echo $value;//降序
}
//arsort这个函数使用后,foreach才能正常使用
代码语言:javascript复制
<?php
$age=array("Bill"=>"60","Steve"=>"56","Mark"=>"31");
asort($age);

foreach ($age as $key => $value) {
	# code...
	echo $key."<br />  ";
	echo $value;//升序
}
//asort这个函数使用后,foreach才能正常使用
代码语言:javascript复制
<?php
$firstname = "Bill";
$lastname = "Gates";
$age = "60";
$result=compact("firstname", "lastname", "age");
print_r($result);//变量变成下标,赋值的是键值
代码语言:javascript复制
<?php
$firstname = "Bill";
$lastname = "Gates";
$age = "60";
$location;
$name = array("firstname", "lastname");
$result = compact($name, "location", "age");

print_r($result);//变量变成下标,赋值的是键值,没有赋值的变量是没有作用的
?>
代码语言:javascript复制
<?php
$cars=array("Volvo","BMW","Toyota");
echo count($cars);//返回元素的数目,从1开始
代码语言:javascript复制
<?php
$cars=array
  (
  "Volvo"=>array
  (
  "XC60",
  "XC90"
  ),
  "BMW"=>array
  (
  "X3",
  "X5"
  ),
  "Toyota"=>array
  (
  "Highlander"
  )//能递归,也就是说能数到二维三维数组即以上
  );
  echo count($cars,1);
代码语言:javascript复制
<?php
$people = array("Bill", "Steve", "Mark", "David");
echo current($people) . "<br>";//这里指向开头的元素
echo next($people) . "<br>";//(下一个)这里指向开头的第二个元素
echo end($people) . "<br>";//指向最后的元素
echo prev($people)."<br>";//这里指向第二个元素(上一个)
echo reset($people)."<br >";//第一个元素
代码语言:javascript复制
<?php
$people = array("Bill", "Steve", "Mark", "David");

reset($people);//指向开头第一个数组元素
print_r(each($people));//输出结果后Bill,往前面移动,也就是"Steve"
echo "<br >";
while (list($key,$val)=each($people)) {
	echo "$key=>$val<br>";//直到$people里面没有值之后,返回false就停止
}
代码语言:javascript复制
<?php
$people = array("Bill", "Steve", "Mark", "David");

echo current($people) . "<br>";//当前指针指向的元素.默认的,就是第一个元素
echo end($people);//最后一个
代码语言:javascript复制
<?php
$my_array = array("a" => "Cat","b" => "Dog", "c" => "Horse");
extract($my_array);
echo "$a=$a;$b=$b;$c=$c";//转义之后,左边变成变量。右边是值
//这个函数把数组左边变成变量名,右边是变量值
代码语言:javascript复制
<?php
$a = "Original";
$my_array = array("a" => "Cat", "b" => "Dog", "c" => "Horse");

extract($my_array, EXTR_PREFIX_SAME, "dup");
//如果某个变量名有冲突,就加dup前缀
echo "$a = $a; $b = $b; $c = $c; $dup_a = $dup_a";
代码语言:javascript复制
<?php
$people = array("Bill", "Steve", "Mark", "David");

if (in_array("23", $people, TRUE))//设置true代表类型 值都要相同
  {
  echo "匹配已找到<br>";
  }
else
  {
  echo "匹配未找到<br>";
  }
if (in_array("Mark",$people, TRUE))
  {
  echo "匹配已找到<br>";
  }
else
  {
  echo "匹配未找到<br>";
  }

if (in_array(23,$people, TRUE))
  {
  echo "匹配已找到<br>";
  }
else
  {
  echo "匹配未找到<br>";
  }
?>
代码语言:javascript复制
<?php
$people = array("Bill", "Steve", "Mark", "David");
echo "键的当前位置是:".key($people);
代码语言:javascript复制
<?php
$age=array("Bill"=>"60","Steve"=>"56","mark"=>"31");
krsort($age);//根据键名对数组进行降序排序(ascll码进行排序的哈举个例子,a=>97......)
foreach($age as $x=>$x_value)
    {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
    }
?>
代码语言:javascript复制
<?php
$age=array("Bill"=>"60","Steve"=>"56","mark"=>"31");
ksort($age);;//根据键名对数组进行升序排序(ascll码进行排序的哈举个例子,a=>97......)
foreach($age as $x=>$x_value)
    {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
    }
?>
代码语言:javascript复制
<?php
$my_array = array("Dog","Cat","Horse");
list($a,$b,$c)=$my_array;//把数组中的键值赋值给list中的变量,依次赋值
echo "I have several animals, a $a, a $b and a $c.";
代码语言:javascript复制
<?php
$temp_files = array("temp15.txt","Temp10.txt",
"temp1.txt","Temp22.txt","temp2.txt");
natsort($temp_files);//区分大小写(升序)大写的就是比小写的大,然后后面业比较数字
print_r($temp_files);
natcasesort($temp_files);
print_r($temp_files);//不区分大小写,(升序),大小写一样,比较的是数字。自然排序
代码语言:javascript复制
<?php
$temp_files = array("temp15.txt","Temp10.txt",
"temp1.txt","Temp22.txt","temp2.txt");
sort($temp_files);
print_r($temp_files);//升序
/*
他们是这样比较的,比如
  array("temp15.txt","Temp10.txt",
"temp1.txt","Temp22.txt","temp2.txt");
比如temp15.txt和temp2.txt,他会怎么比呢?temp1与temp2那个大?那个大,就那个靠前,这样子的啦
 */
代码语言:javascript复制
<?php
$people = array("Bill", "Steve", "Mark", "David");
echo pos($people);//pos的意思相当于current()
代码语言:javascript复制
<?php
$number=range(0,5);
print_r($number);//0到5这个范围,包括0和5
代码语言:javascript复制
<?php
$number=range(0,5,2);
print_r($number);//数字只在0到5这个范围,包括0和5。递增的速度是 2
代码语言:javascript复制
<?php
$cars=array("Volvo","BMW","Toyota");
rsort($cars);//逆向排序后,需要count获取总数,然后for遍历出来就行
$length=count($cars);
for($x=0;$x<$length;$x  )
   {
   echo $cars[$x];
   echo "<br>";
   }
?>
代码语言:javascript复制
<?php
$my_array = array("red","green","blue","yellow","purple");
shuffle($my_array);
print_r($my_array);//对数组之后的元素随机排序,刷新一次一次效果哦
代码语言:javascript复制
<?php
$my_array = array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow","e"=>"purple");shuffle($my_array);
print_r($my_array);//对数组之后的元素随机排序,针对的是键值
代码语言:javascript复制
<?php
$cars=array
  (
  "Volvo"=>array
  (
  "XC60",
  "XC90"
  ),
  "BMW"=>array
  (
  "X3",
  "X5"
  ),
  "Toyota"=>array
  (
  "Highlander"
  )
  );

echo "常规计数:" . sizeof($cars)."<br>";
echo "递归计数:" . sizeof($cars,1);//二三维数组都可以遍历,count()的别名
?>
代码语言:javascript复制
<?php
$cars=array("Volvo","BMW","Toyota");
sort($cars);//对字母进行排序,a每一个元素的scll码想加起来
$clength=count($cars);
for($x=0;$x<$clength;$x  )
   {
   echo $cars[$x];
   echo "<br>";
   }
?>
代码语言:javascript复制
<?php
function my_sort($a,$b)
{
if ($a==$b) return 0;
return ($a<$b)?-1:1;
}

$arr=array("a"=>4,"b"=>2,"c"=>8,d=>"6");
uasort($arr, "my_sort");
foreach($arr as $x=>$x_value)
    {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
    }//根据键值进行升序,不区分类型
?>
代码语言:javascript复制
<?php
function my_sort($a,$b)
{
if ($a==$b) return 0;
return ($a<$b)?-1:1;
}

$arr=array("a"=>4,"b"=>2,"c"=>8,d=>"6");
uksort($arr,"my_sort");
foreach($arr as $x=>$x_value)//根据键名进行排序。ascll。比如a=97 b=98.....升序
    {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
    }
?>
代码语言:javascript复制
<?php
function my_sort($a,$b)
{
if ($a==$b) return 0;
   return ($a<$b)?-1:1;
}

$a=array(4,2,8,6);
usort($a,"my_sort");

$arrlength=count($a);
for($x=0;$x<$arrlength;$x  )
   {
   echo $a[$x];//对元素进行排序,先函数,然后count下,然后遍历
   echo "<br>";
   }
?>

0 人点赞