PHP simple implementation of two-dimensional array assignment and traversal function example, two-dimensional array assignment
The example in this article describes PHP's simple function of assigning values to and traversing two-dimensional arrays. We will share this with you for your reference. The details are as follows:
Example 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; $ I <count ($ loptop); $ I ++ ){ // Echo "No.: $ loptop [$ I] [lid]"; // error // echo "No :". $ loptop [$ I] ['lid']; // correct, but $ tmp = $ loptop [$ I]; echo "No: $ tmp [lid] <br/> "; echo" Image: $ tmp [pic] <br/> "; echo" title: $ tmp [title] <br/> "; echo" price: $ tmp [price] <br/> "; echo" special offer: $ tmp [isOnSale] <br/> "; echo" shelving time :". date ("Y-m-d H: I: s", $ tmp ['shelftime']). "<br/>" ;}?>
Running result:
No.: 1000 image: img/1.png title: L1 price: 5000 special offer: 1 shelving time: 06:55:56 No.: 1001 image: img/2.png title: L2 price: 5000 special offer: 1 shelving time: 18:07:36 No.: 1002 image: img/3.png title: L3 price: 5000 whether special offer: 1 shelving time: 20:02:36 No.: 1003 image: img/4.png title: l4 price: 5000 special offer: 1 shelving time: 15:02:36
Example 2:
<? Php $ stu1 ['sid '] = 1000; $ stu1 ['username'] = "abc1"; $ stu1 ['Password'] = "123456 "; $ stu1 ['e-mail '] = "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 ['e-mail '] = "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 ['e-mail '] = "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 ['e-mail '] =" 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; $ I <count ($ stu); $ I ++) {$ tmp = $ stu [$ I]; echo "No.: $ tmp [sid] <br/> "; echo "userName: $ tmp [userName] <br/>"; echo "passWord: $ tmp [passWord] <br/>"; echo "email: $ tmp [email] <Br/> "; echo" cell phone: $ tmp [tel] <br/> "; echo" Avatar: $ tmp [headScu] <br/> "; if ($ tmp ['sex'] = "M") {echo "Gender: male <br/> ";} if ($ tmp ['sex'] = "F") {echo "Gender: female <br/>";} echo "registration time :". date ('Y-m-d H: I: s', $ tmp ['regtime']). "<br/>"; if ($ tmp ['isonline'] = 1) {echo "status: Online <br/> ";} if ($ tmp ['isonline'] = 0) {echo "status: offline <br/>" ;}}?>
Running result:
No.: 1000 User name: abc1 password: 123456 mailbox: 2109882885@qq.com mobile phone: 15700769164 Avatar: stu1.png Gender: male registration time: 09:10:35 status: Online No.: 1001 User name: abc2 password: 123456 mailbox: 2109882886@qq.com mobile phone: 15700769165 Avatar: stu2.png Gender: male registration time: 01:49:04 status: Online No.: 1002 Username: abc3 password: 123456 mailbox: 2109882887@qq.com mobile phone: 15700769166 Avatar: stu3.png gender: male registration time: 03:01:11 Status: Not online number: 1003 Username: abc4 password: 123456 mailbox: 2109882888@qq.com mobile phone: 15700769167 Avatar: stu4.png Gender: Female registration time: 14:55:34 status: Online