亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

創(chuàng)建數(shù)組,分別用for/while和foreach遍歷,比較他們的不同之處

Original 2019-04-08 09:45:07 277
abstract:<?php //索引數(shù)組 //用字面量的方式進行創(chuàng)建:Index $Index = ['a','b','c'];//create //關(guān)聯(lián)數(shù)組 $Relation = ['name'=>'Zhang','position'=>'Chin
<?php
//索引數(shù)組
//用字面量的方式進行創(chuàng)建:Index
$Index = ['a','b','c'];//create

//關(guān)聯(lián)數(shù)組
$Relation = ['name'=>'Zhang','position'=>'China','skill'=>'code'];

//逐個添加的方式創(chuàng)建數(shù)組
$Relations=[];//先聲明
$Relations['name']='Wang';
$Relations['position']='America';
$Relations['skill']='code';

//for遍歷索引數(shù)組
$res1='';//res一般是空字符串
for($i=0;$i<count($Index);$i++){
    $res1 .=$Index[$i] .',';
}

echo rtrim($res1,','), '<hr>';

//while循環(huán)遍歷索引數(shù)組
$res2='';
$i=0;
while($i<count($Index)){
    $res2.=$Index[$i].'*';
    $i++;
}
echo rtrim($res2,'*'),'<hr>';

//foreach循環(huán)遍歷索引數(shù)組
foreach($Relation as $key=>$value){
    echo $key,'=>',$value,'<br>';
}

for內(nèi)部直接比較數(shù)組的長度,while常常需要條件判斷數(shù)組是否執(zhí)行到最后一個數(shù)據(jù)。foreach是專門以鍵和值為中心,為遍歷數(shù)組設(shè)計的函數(shù)。

Correcting teacher:天蓬老師Correction time:2019-04-08 09:46:55
Teacher's summary:每一種循環(huán)都有自己的應(yīng)用場景, 但絕大多數(shù)情況 下, 都可以互相轉(zhuǎn)換的, 最靈活最復(fù)雜的當(dāng)屬for循環(huán), 不可小看

Release Notes

Popular Entries