php入門學(xué)習(xí)知識(shí)點(diǎn)五關(guān)于php數(shù)組的幾個(gè)基本操作

字號(hào):


    php入門學(xué)習(xí)知識(shí)點(diǎn)五關(guān)于php數(shù)組的幾個(gè)基本操作,數(shù)組是php中最常用的,所以大家一定要熟練的掌握。
    代碼如下:
    <?php
    /*
    *簡(jiǎn)單的數(shù)組定義與訪問(wèn)
    */
    echo"簡(jiǎn)單的數(shù)組定義與訪問(wèn)<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"通過(guò)for循環(huán)進(jìn)行數(shù)組遍歷<br>";
    echo"############################################################<br>";
    for($index=0;$index<count($address);$index++){
    print("數(shù)組中第".$index."個(gè)的地區(qū)$address[$index]為<br>");
    }
    echo"############################################################<br><br><br>";
    /*
    *數(shù)組初始化
    */
    echo"數(shù)組初始化,并通過(guò)日期函數(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)時(shí)區(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ù)組初始化,并定義鍵,然后通過(guò)鍵值訪問(wèn)數(shù)組
    */
    echo"數(shù)組初始化,并定義鍵,然后通過(guò)鍵訪問(wèn)數(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"通過(guò)英文縮寫(xiě)Aug訪問(wèn)數(shù)組".$arrMonth["Aug"]."<br>";
    echo"############################################################<br><br><br>";
    echo"下面通過(guò)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]
    ?>