代碼如下:
<?php /* * 簡單的數組定義與訪問 */ echo "簡單的數組定義與訪問<br>"; echo "############################################################<br>"; $address=array(5); $address[0]="福州"; $address[1]="廈門"; $address[2]="漳州"; $address[3]="泉州"; $address[4]="寧德"; $address[5]="南平"; $address[6]="龍岩"; echo "我現在住在$address[1]<br>"; echo "############################################################<br><br><br>"; /* * 數組遍曆 */ echo "通過for迴圈進行數組遍曆<br>"; echo "############################################################<br>"; for($index=0;$index<count($address);$index++){ print("數組中第".$index."個的地區$address[$index]為<br>"); } echo "############################################################<br><br><br>"; /* * 數組初始化 */ echo "數組初始化,並通過日期函數得到當前月份的數字,輸出相關數組下標的內容<br>"; echo "############################################################<br>"; $arrMonth=array("January","February","March","April","May","June","July","August","September","October","November","December"); date_default_timezone_set("utc"); //設定預設時區 $month=date("m"); echo "數組結構為"; print_r($arrMonth); echo "當前是第".$month."月,他的英文是".$arrMonth[$month-1]."<br>"; echo "############################################################<br><br><br>"; /* *數組初始化,並定義鍵,然後通過索引值訪問數組 */ echo "數組初始化,並定義鍵,然後通過鍵訪問數組<br>"; echo "############################################################<br>"; $arrMonth=array("Jan"=>"January","Feb"=>"February","Mar"=>"March","Apr"=>"April","May"=>"May","Jun"=>"June","Jul"=>"July" ,"Aug"=>"August","Sept"=>"Septmber","Oct"=>"October","Nov"=>"November","Dec"=>"December" ); echo "通過英文縮寫Aug 訪問數組".$arrMonth["Aug"]."<br>"; echo "############################################################<br><br><br>"; echo "下面通過Foreach遍曆數組<br>"; echo "############################################################<br>"; foreach ($arrMonth as $key=>$value){ echo " =>鍵是$key,值是$value<br>"; } echo "############################################################<br><br><br>"; /* * 定義多維陣列 */ echo "定義二維數組<br>"; $arrArea=array("華東地區"=>array("福建","浙江"),"華北地區"=>array("北京","天津")); echo "華東地區=>".$arrArea["華東地區"][0] ?>