1.创建并输出数组
(1)相同数据类型的数组
$attr=array(1,2,3,4,5);
print_r($attr);
echo "<br>";
$sttr1=array("a","b","c","d");
print_r($sttr1);
echo "<br>";
(2)创建关联数组
$sttr2=array("one"=>1,"two"=>2,"three"=>3);
print_r($sttr2);
2.统计数组中元素个数 count()函数
$sttr2=array("one"=>1,"two"=>2,"three"=>3);
echo count($sttr2);//输出3
3.向数组中添加元素 $sttr[]="";
$sttr1=array("a","b","c","d");
$sttr1[]="e";
print_r($sttr1);
4.将数组中指定索引位置的元素替换 $array[n]="";
$sttr1=array("a","b","c","d");
$sttr1[2]="e";
print_r($sttr1);
5.获取数组中最后一个元素 array_pop()函数;
$sttr=array("a","b","c","d");
$sttr1=array_pop($sttr);
print_r($sttr1);//输出d
6.去除数组中的重复元素 array_unique()函数
$sttr=array("a","b","c","d","b","c");
$sttr1=array_unique($sttr);
print_r($sttr1);
7.对数组中的元素进行随机抽取 rand()函数
$sttr=array("a","b","c","d","b","c");
$r=rand(0,5);
echo "随机取到的元素是:".$sttr[$r];
8.二维数组的输出 print_r($sttr[键值名])
$sttr=array("one"=>array("a","b"),"two"=>array("c","d"),"three"=>array("e","f"));
print_r($sttr["one"]);
9.获取数组当前的键名和值 用foreach()遍历
$sttr=array("one"=>111,"two"=>222,"three"=>3333);
foreach($sttr as $key=>$value)
{
echo $key;//输出键名 }
echo "<br>";
foreach($sttr as $key=>$value)
{
echo $value;//输出值 }
10.检测数组中是否存在某个值 array_key_exists(键名,数组名)函数
$sttr=array("one"=>111,"two"=>222,"three"=>3333);
if(array_key_exists('one',$sttr))
{
echo "存在";
}
else
{
echo "不存在";
}
11.合并数组 array_merge()函数
$sttr=array("one"=>111,"two"=>222,"three"=>3333);
$sttr1=array("a"=>444,"b"=>666,"c"=>777);
$sttr2=array_merge($sttr,$sttr1);
print_r($sttr2);
12.for循环遍历,只能遍历索引数组
$attr = array(1,2,3);
for($i=0;$i<count($attr);$i++)
{
echo $attr[$i]."<br>";
}
13.foreach遍历,索引关联都可以遍历
$attr = array(1,2,3);
$attr1 = array("one"=>1,"two"=>2,"3"=>3); foreach($attr as $v)
{
echo $v."<br>";
} foreach($attr1 as $k=>$v)
{
echo "{$k}--{$v}<br>";
}
14.each()和list()配合着来遍历数组
$attr1 = array("one"=>1,"two"=>2,"3"=>3);
var_dump(each($attr1)); //取数组里面当前指针指向的元素
如果需要数组中所有数据,需要一步一步的写
var_dump(each($attr1));
var_dump(each($attr1));
$attr = array(1,2,3);
list($a,$b,$c)=$attr; //将右侧数组里面的元素赋值给参数列表里面的变量
echo $a,$b,$c;
用each()和list()结合着遍历数组:
$attr1 = array("one"=>1,"two"=>2,"3"=>3); while(list($k,$v) = each($attr1))
{ echo "{$k}--{$v}<br>"; }
15.使用指针的方式来遍历数组
$attr1 = array("one"=>1,"two"=>2,"3"=>3)
echo current($attr1); //取指针指向的当前元素的value值 echo key($attr1); //取指针指向的当前元素的key
next($attr1); //将指针向下调一个 prev($attr1); //将指针向上调一个 end($attr1); //将指针调向最后一个元素 reset($attr1); //将指针复位
指针思想遍历集合:
$attr1 = array("one"=>1,"two"=>2,"3"=>3); for($i=0;$i<count($attr1);$i++)
{
echo key($attr1);
next($attr1);
} do
{
echo key($attr1);
}
while(next($attr1))
16获取数组的长度 - count() 函数 count() 函数用于返回数组的长度(元素的数量):
<?php
$cars=array("沃尔沃","宝马","丰田");
echo count($cars);
?>
17.数组的排序:
1.sort() - 对数组进行升序排列
$sttr=array("2","1","3");
sort($sttr);
foreach($sttr as $aa)
{
echo $aa;
}//输出123
2.rsort() - 对数组进行降序排列
$sttr=array("2","1","3");
rsort($sttr);
foreach($sttr as $aa)
{
echo $aa;
}//输出321
3.asort() - 根据关联数组的值,对数组进行升序排列
$age=array("Peter"=>"37","Ben"=>"35","Joe"=>"43");
asort($age);
foreach($age as $aa)
{
echo $aa;
}
4.ksort() - 根据关联数组的键,对数组进行升序排列
$age=array("5"=>"35","7"=>"37","2"=>"43");
ksort($age); foreach($age as $aa)
{
echo $aa;
}
5.arsort() - 根据关联数组的值,对数组进行降序排列
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
arsort($age); foreach($age as $aa)
{
echo $aa;
}
6.krsort() - 根据关联数组的键,对数组进行降序排列
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
krsort($age);
foreach($age as $aa)
{
echo $aa;
}