//讀取數組
$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 "<br/>";//在數組頭添加元素
$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 <br/>",$key);
next($oldarr); //每次key不會移動指針,需要next來移動指針
}//獲取數組中的"值"
$oldarr=array("力量"=>"拳擊","速度"=>"跑步");
while($val = current($oldarr)){
printf("%s <br/>",$val);
next($oldarr); //每次key不會移動指針,需要next來向後移動指針 prev()向前移動一個 reset()是移動首位 end()是移動末尾
}//歷遍數組中的"鍵和值"
$arr=array("one", "two", "three");
foreach ($arr as $value){
echo "Value: " . $value . "<br />";
}//統計數組元素出現頻率(結果是值數量)
$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<count($arr);$i++){
$randnum = rand(0,50);
if(in_array($randnum,$arr)){
break;
}else{
printf("不存在 %d <br/>",$randnum);
}