php入門學(xué)習(xí)知識點五關(guān)于php數(shù)組的幾個基本操作,數(shù)組是php中最常用的,所以大家一定要熟練的掌握。
代碼如下:
<?php
/*
*簡單的數(shù)組定義與訪問
*/
echo"簡單的數(shù)組定義與訪問<br>";
echo"############################################################<br>";
$address=array(5);
$address[0]="福州";
$address[1]="廈門";
$address[2]="漳州";
$address[3]="泉州";
$address[4]="寧德";
$address[5]="南平";
$address[6]="龍巖";
echo"我現(xiàn)在住在$address[1]<br>";
echo"############################################################<br><br><br>";
/*
*數(shù)組遍歷
*/
echo"通過for循環(huán)進(jìn)行數(shù)組遍歷<br>";
echo"############################################################<br>";
for($index=0;$index<count($address);$index++){
print("數(shù)組中第".$index."個的地區(qū)$address[$index]為<br>");
}
echo"############################################################<br><br><br>";
/*
*數(shù)組初始化
*/
echo"數(shù)組初始化,并通過日期函數(shù)得到當(dāng)前月份的數(shù)字,輸出相關(guān)數(shù)組下標(biāo)的內(nèi)容<br>";
echo"############################################################<br>";
$arrMonth=array("January","February","March","April","May","June","July","August","September","October","November","December");
date_default_timezone_set("utc");//設(shè)置默認(rèn)時區(qū)
$month=date("m");
echo"數(shù)組結(jié)構(gòu)為";
print_r($arrMonth);
echo"當(dāng)前是第".$month."月,他的英文是".$arrMonth[$month-1]."<br>";
echo"############################################################<br><br><br>";
/*
*數(shù)組初始化,并定義鍵,然后通過鍵值訪問數(shù)組
*/
echo"數(shù)組初始化,并定義鍵,然后通過鍵訪問數(shù)組<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訪問數(shù)組".$arrMonth["Aug"]."<br>";
echo"############################################################<br><br><br>";
echo"下面通過Foreach遍歷數(shù)組<br>";
echo"############################################################<br>";
foreach($arrMonthas$key=>$value){
echo" =>鍵是$key,值是$value<br>";
}
echo"############################################################<br><br><br>";
/*
*定義多維數(shù)組
*/
echo"定義二維數(shù)組<br>";
$arrArea=array("華東地區(qū)"=>array("福建","浙江"),"華北地區(qū)"=>array("北京","天津"));
echo"華東地區(qū)=>".$arrArea["華東地區(qū)"][0]
?>
更多信息請查看IT技術(shù)專欄