本文實例講述了PHP簡單實現(xiàn)二維數(shù)組賦值與遍歷功能。分享給大家供大家參考,具體如下:
示例1:
?php
$loptop1['lid'] = 1000;
$loptop1['pic'] = 'img/1.png';
$loptop1['title'] = 'L1';
$loptop1['price'] = 5000;
$loptop1['isOnSale'] = 1;
$loptop1['shelfTime'] = 1234556;
$loptop2['lid'] = 1001;
$loptop2['pic'] = 'img/2.png';
$loptop2['title'] = 'L2';
$loptop2['price'] = 5000;
$loptop2['isOnSale'] = 1;
$loptop2['shelfTime'] = 123444456;
$loptop3['lid'] = 1002;
$loptop3['pic'] = 'img/3.png';
$loptop3['title'] = 'L3';
$loptop3['price'] = 5000;
$loptop3['isOnSale'] = 1;
$loptop3['shelfTime'] = 1243454556;
$loptop4['lid'] = 1003;
$loptop4['pic'] = 'img/4.png';
$loptop4['title'] = 'L4';
$loptop4['price'] = 5000;
$loptop4['isOnSale'] = 1;
$loptop4['shelfTime'] = 1234364556;
$loptop[0] = $loptop1;
$loptop[1] = $loptop2;
$loptop[2] = $loptop3;
$loptop[3] = $loptop4;
for($i=0;$icount($loptop);$i++){
//echo "編號:$loptop[$i][lid]"; //錯誤
//echo "編號:" . $loptop[$i]['lid']; //正確,但不推薦
$tmp = $loptop[$i];
echo "編號:$tmp[lid]br/>";
echo "圖片:$tmp[pic]br/>";
echo "標(biāo)題:$tmp[title]br/>";
echo "價格:$tmp[price]br/>";
echo "是否特價:$tmp[isOnSale]br/>";
echo "上架時間:" . date("Y-m-d H:i:s",$tmp['shelfTime']) . "br/>";
}
?>
運(yùn)行結(jié)果:
編號:1000
圖片:img/1.png
標(biāo)題:L1
價格:5000
是否特價:1
上架時間:1970-01-15 06:55:56
編號:1001
圖片:img/2.png
標(biāo)題:L2
價格:5000
是否特價:1
上架時間:1973-11-29 18:07:36
編號:1002
圖片:img/3.png
標(biāo)題:L3
價格:5000
是否特價:1
上架時間:2009-05-27 20:02:36
編號:1003
圖片:img/4.png
標(biāo)題:L4
價格:5000
是否特價:1
上架時間:2009-02-11 15:02:36
示例2:
?php
$stu1['sid'] = 1000;
$stu1['userName'] = "abc1";
$stu1['passWord'] = "123456";
$stu1['email'] = "2109882885@qq.com";
$stu1['tel'] = "15700769164";
$stu1['headScu'] = "stu1.png";
$stu1['sex'] = "M";
$stu1['regTime'] = 1111223435;
$stu1['isOnline'] = 1;
$stu2['sid'] = 1001;
$stu2['userName'] = "abc2";
$stu2['passWord'] = "123456";
$stu2['email'] = "2109882886@qq.com";
$stu2['tel'] = "15700769165";
$stu2['headScu'] = "stu2.png";
$stu2['sex'] = "M";
$stu2['regTime'] = 122435344;
$stu2['isOnline'] = 1;
$stu3['sid'] = 1002;
$stu3['userName'] = "abc3";
$stu3['passWord'] = "123456";
$stu3['email'] = "2109882887@qq.com";
$stu3['tel'] = "15700769166";
$stu3['headScu'] = "stu3.png";
$stu3['sex'] = "M";
$stu3['regTime'] = 3463464567;
$stu3['isOnline'] = 0;
$stu4['sid'] = 1003;
$stu4['userName'] = "abc4";
$stu4['passWord'] = "123456";
$stu4['email'] = "2109882888@qq.com";
$stu4['tel'] = "15700769167";
$stu4['headScu'] = "stu4.png";
$stu4['sex'] = "F";
$stu4['regTime'] = 235234534;
$stu4['isOnline'] = 1;
$stu = [$stu1,$stu2,$stu3,$stu4];
for($i=0;$icount($stu);$i++){
$tmp = $stu[$i];
echo "編號:$tmp[sid]br/>";
echo "用戶名:$tmp[userName]br/>";
echo "密碼:$tmp[passWord]br/>";
echo "郵箱:$tmp[email]br/>";
echo "手機(jī):$tmp[tel]br/>";
echo "頭像:$tmp[headScu]br/>";
if($tmp['sex'] == "M"){
echo "性別:男br/>";
}
if($tmp['sex'] == "F"){
echo "性別:女br/>";
}
echo "注冊時間:" . date('Y-m-d H:i:s',$tmp['regTime']) . "br/>";
if($tmp['isOnline'] == 1){
echo "狀態(tài):在線br/>";
}
if($tmp['isOnline'] == 0){
echo "狀態(tài):不在線br/>";
}
}
?>
運(yùn)行結(jié)果:
編號:1000
用戶名:abc1
密碼:123456
郵箱:2109882885@qq.com
手機(jī):15700769164
頭像:stu1.png
性別:男
注冊時間:2005-03-19 09:10:35
狀態(tài):在線
編號:1001
用戶名:abc2
密碼:123456
郵箱:2109882886@qq.com
手機(jī):15700769165
頭像:stu2.png
性別:男
注冊時間:1973-11-18 01:49:04
狀態(tài):在線
編號:1002
用戶名:abc3
密碼:123456
郵箱:2109882887@qq.com
手機(jī):15700769166
頭像:stu3.png
性別:男
注冊時間:1943-08-27 03:01:11
狀態(tài):不在線
編號:1003
用戶名:abc4
密碼:123456
郵箱:2109882888@qq.com
手機(jī):15700769167
頭像:stu4.png
性別:女
注冊時間:1977-06-15 14:55:34
狀態(tài):在線
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP數(shù)組(Array)操作技巧大全》、《PHP常用遍歷算法與技巧總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計算法總結(jié)》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
您可能感興趣的文章:- PHP實現(xiàn)二維數(shù)組(或多維數(shù)組)轉(zhuǎn)換成一維數(shù)組的常見方法總結(jié)
- PHP實現(xiàn)通過二維數(shù)組鍵值獲取一維鍵名操作示例
- php利用array_search與array_column實現(xiàn)二維數(shù)組查找
- PHP實現(xiàn)二維數(shù)組按照指定的字段進(jìn)行排序算法示例
- php二維數(shù)組按某個鍵值排序的實例講解
- PHP實現(xiàn)二維數(shù)組中的查找算法小結(jié)
- PHP實現(xiàn)一維數(shù)組與二維數(shù)組去重功能示例
- PHP簡單實現(xiàn)二維數(shù)組的矩陣轉(zhuǎn)置操作示例
- PHP二維數(shù)組分頁2種實現(xiàn)方法解析