- 相關(guān)推薦
關(guān)于php數(shù)組的幾個基本操作
數(shù)組是php中最常用的,所以大家一定要熟練的掌握。下面是小編為大家搜集整理出來的有關(guān)于php數(shù)組的幾個基本操作,一起了解一下吧!
代碼如下:
<?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 ($arrMonth as $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]
?>
【php數(shù)組的幾個基本操作】相關(guān)文章:
PHP自帶的幾個實(shí)用的數(shù)組函數(shù)詳解10-07
Java數(shù)組的基本操作方法介紹08-14
PHP數(shù)組函數(shù)知識10-24
PHP超全局?jǐn)?shù)組08-16
PHP數(shù)組面試考題05-17
PHP數(shù)組長度的技巧09-18
php數(shù)組長度的方法10-05
PHP操作MySQL數(shù)據(jù)庫的基本類10-14
php遞歸遍歷多維數(shù)組的方法10-06