//读取数组
$arr=array("xx"=>"叉叉","yy"=>"歪歪");
echo "读出第一个:".$arr[xx];//统计数组个数
$array=array(aaa,bbb,ccc,ddd);
echo "数组个数为:".count($array)."n"; //计算数组单元数目或对象中属性个数 等同sizeof()//快速创建数组
$arr_range = range(0,20,2); //定义值的范围 最小0 最大6 步长为2(之差,可选)
$arr_range2 = range("A","N");
echo "用range()快速创建数字数组:";
print_r($arr_range);
echo "n";
echo "用range()快速创建字母数组:";
print_r($arr_range2);
echo "n";//测试数组
if(is_array($arr_range)){ echo "$arr_range是数组!n"; }else{echo "不是数组!n";}//拆分数组
$str="小猪,小猫,小狗";
list($a,$b,$c) =explode(",",$str);
echo "数组拆分为:".$a ." ". $b ." ". $c;
echo "
";//在数组头添加元素
$oldarr=array("小张","小李","小王","小赵");
$newarr=array_unshift($oldarr,"小孙","小周","小武");
print_r($oldarr); //这个数组变量在头部,被加入新的
echo "个数为:".print_r($newarr);//在数组尾部添加元素
$oldarr=array("小张","小李","小王","小赵");
$newarr=array_push($oldarr,"小孙","小周","小武");
print_r($oldarr); //这个数组变量在尾部,被加入新的
echo "个数为:".print_r($newarr);//从数组头删除值
$oldarr=array("小张","小李","小王","小赵");
$newarr=array_shift($oldarr);
print_r($newarr); //返回删除的值
print_r($oldarr); //返回被删除的后剩余的//从数组尾部删除值
$oldarr=array("小张","小李","小王","小赵");
$newarr=array_pop($oldarr);
print_r($newarr); //返回删除的值
print_r($oldarr); //返回被删除的后剩余的//定位元素:搜索数组
$search ="小刘";
$oldarr =array("小张","小李","小王","小赵","小刘");
if(in_array($search,$oldarr)){ echo "数组中有:".$search."n";}else{echo "数组中没有:".$search."n";}//定位元素:搜索关联数组"键"
$oldarr["水果1"]="西瓜";
$oldarr["水果2"]="桃子";
$oldarr["水果3"]="葡萄";
if(array_key_exists("水果2",$oldarr)){ printf("你选的水果是: %s n",$oldarr["水果2"]);} else {echo "不存在这个!";}//定位元素:搜索关联数组"值"
$oldarr["水果1"]="西瓜";
$oldarr["水果2"]="桃子";
$oldarr["水果3"]="葡萄";
$found=array_search("西瓜",$oldarr);
if($found){ printf("%s 的值是: %s n",$found,$oldarr[$found]);} else {echo "不存在这个!";}//获取数组中的"键"
$oldarr=array("力量"=>"拳击","速度"=>"跑步");
while($key = key($oldarr)){
printf("%s
",$key);
next($oldarr); //每次key不会移动指针,需要next来移动指针
}//获取数组中的"值"
$oldarr=array("力量"=>"拳击","速度"=>"跑步");
while($val = current($oldarr)){
printf("%s
",$val);
next($oldarr); //每次key不会移动指针,需要next来向后移动指针 prev()向前移动一个 reset()是移动首位 end()是移动末尾
}//历遍数组中的"键和值"
$arr=array("one", "two", "three");
foreach ($arr as $value){
echo "Value: " . $value . "
";
}//统计数组元素出现频率(结果是值数量)
$arr=array("one", "two", "three", "two", "three");
print_r(array_count_values($arr));//确定唯一的数组元素(输出不重复的内容)
$arr=array("one", "two", "three", "two", "three");
print_r(array_unique($arr));//逆置数组元素顺序(键和值)
$arr=array("one", "two", "three", "four", "five");
print_r(array_reverse($arr));//置换数组和值
$arr=array("one", "two", "three", "four", "five");
print_r(array_flip($arr));//正序数组排序(键和值关联不在保持)
$arr=array("one", "two", "three", "four", "five");
sort($arr);
print_r($arr);//正序数组排序(键和值关联保持)
$arr=array("one", "two", "three", "four", "five");
asort($arr);
print_r($arr);//逆序数组排序(键和值关联不保持)
$arr=array("one", "two", "three", "four", "five");
rsort($arr);
print_r($arr);//逆序数组排序(键和值关联保持)
$arr=array("one", "two", "three", "four", "five");
arsort($arr);
print_r($arr);//自然排序数组(经典排序)
$arr=array("a1", "a3", "a10", "a22", "a19");
natsort($arr);
print_r($arr);//自然排序数组(经典排序,不区分大小写的)
$arr=array("A1", "a3", "a10", "A22", "a19");
natcasesort($arr);
print_r($arr);//按键值对数组排序
//ksort();
//以逆序对数组键排序
//krsort();
//根据用户自定义规则排序
$arr=array('2009-02-06','2009-02-10','2009-02-13','10-06-2009','9-17-2009');
sort($arr);
print_r($arr);//合并、拆分、接合、分解数组(递归合并一个或多个数组,注意键的引号)
$arr_a = array("John"=>100,"James"=>85);
$arr_b = array("Micky"=>78,"John"=>45);
$newarr = array_merge_recursive($arr_a,$arr_b);
print_r($newarr);//递归追加数组
$arr_a = array("AK"=>"xx","AV","AO","AE"); //用第一个数组的键
$arr_b = array("Akssjd"=>"xasd","Aosdwe","Aesadlkj","Avxiwqlk"); //用第二个数组的值
$newarr = array_combine($arr_a,$arr_b);
print_r($newarr);//链接两个数组(键自动顺序数字,值合并,有键的的单独显示,键相同值会覆盖)
$arr_a = array("W",a=>"O","F","R","C");
$arr_b = array("2","6","4","7","3",a=>"x","0","9");
$newarr = array_merge($arr_a,$arr_b);
shuffle($newarr); //洗牌(将数组打乱),每次结果不一样
print_r($newarr);//拆分数组
$oldarr=array("西瓜","苹果","柚子","香蕉","芒果","奇异果","山楂");
$newarr=array_slice($oldarr,2,-2); //去掉前两个,后两个
print_r($newarr); //新的数组
print_r($oldarr); //全部原始数组//接合数组(形成新两个数组)
$oldarr=array("西瓜","苹果","柚子","香蕉","芒果","奇异果","山楂");
$newarr=array_splice($oldarr,2);
print_r($newarr); //新的数组
print_r($oldarr); //被删除的数组//求数组的交际
$new_a=array("A","B","C","D","E","F"); //以"键和值"第一行数组为标准,3个数组"值"有完全相同的输出
$new_b=array("B","X","E","R","W","P");
$new_c=array("Z","N","B","W","E","C");
$newarr = array_intersect($new_a,$new_b,$new_c);
print_r($newarr);//求关联数组的交集(类似上边这个)
//array_intersect_assoc() 这个考虑"键"是都也相同,如果"键"和"值"都相同输出结果
//求数组的差集(这个与intersect()正好相反)
$new_a=array("A","B","C","D","E","F"); //以"键和值"第一行数组为标准,3个数组"值"有完全没有的输出
$new_b=array("B","X","E","R","W","P");
$new_c=array("Z","N","B","W","E","C");
$newarr = array_diff($new_a,$new_b,$new_c);
print_r($newarr);//求关联数组的交集(类似上边这个)
//array_diff_assoc() 这个考虑"键"是都也相同,如果"键"和"值"都没有输出结果
//返回一组随即的"键"
$oldarr = array("aaa"=>"dasd","bbb"=>"axsae","ccc"=>"fdvdf","ddd"=>"ewmcc");
$newarr = array_rand($oldarr,2);
print_r($newarr);//随机洗牌数组,随机打乱"值"顺序
$oldarr = array(a=>"sd","sc","xe","po","ed");
shuffle($oldarr);
print_r($oldarr);//对数组中的值求和
$oldarr = array(36,"hello",75);
$newarr = array_sum($oldarr);
print $newarr;//将数组分解为多维数组
$oldarr=array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R");//shuffle($oldarr); //洗下牌
$newarr=array_chunk($oldarr,4);
print_r($newarr);
/*找到数组里不存在的数字 */
$arr =array(2,3,5,7,9,11,13,14,16,23,26,28,29,31,32,33,35,37,39,41,46,49);
for($i=0;$i
if(in_array($randnum,$arr)){
break;
}else{
printf("不存在 %d
",$randnum);
}