本文实例讲述了php简单实现二维数组赋值与遍历功能。分享给大家供大家参考,具体如下:
示例1:
$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
//echo "编号:$loptop[$i][lid]"; //错误
//echo "编号:" . $loptop[$i]['lid']; //正确,但不推荐
$tmp = $loptop[$i];
echo "编号:$tmp[lid]
";
echo "图片:$tmp[pic]
";
echo "标题:$tmp[title]
";
echo "价格:$tmp[price]
";
echo "是否特价:$tmp[isonsale]
";
echo "上架时间:" . date("y-m-d h:i:s",$tmp['shelftime']) . "
";
}
?>
运行结果:
编号:1000
图片:img/1.png
标题:l1
价格:5000
是否特价:1
上架时间:1970-01-15 06:55:56
编号:1001
图片:img/2.png
标题:l2
价格:5000
是否特价:1
上架时间:1973-11-29 18:07:36
编号:1002
图片:img/3.png
标题:l3
价格:5000
是否特价:1
上架时间:2009-05-27 20:02:36
编号:1003
图片:img/4.png
标题:l4
价格:5000
是否特价:1
上架时间:2009-02-11 15:02:36
示例2:
$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;$i
$tmp = $stu[$i];
echo "编号:$tmp[sid]
";
echo "用户名:$tmp[username]
";
echo "密码:$tmp[password]
";
echo "邮箱:$tmp[email]
";
echo "手机:$tmp[tel]
";
echo "头像:$tmp[headscu]
";
if($tmp['sex'] == "m"){
echo "性别:男
";
}
if($tmp['sex'] == "f"){
echo "性别:女
";
}
echo "注册时间:" . date('y-m-d h:i:s',$tmp['regtime']) . "
";
if($tmp['isonline'] == 1){
echo "状态:在线
";
}
if($tmp['isonline'] == 0){
echo "状态:不在线
";
}
}
?>
运行结果:
编号:1000
用户名:abc1
密码:123456
邮箱:2109882885@qq.com
手机:15700769164
头像:stu1.png
性别:男
注册时间:2005-03-19 09:10:35
状态:在线
编号:1001
用户名:abc2
密码:123456
邮箱:2109882886@qq.com
手机:15700769165
头像:stu2.png
性别:男
注册时间:1973-11-18 01:49:04
状态:在线
编号:1002
用户名:abc3
密码:123456
邮箱:2109882887@qq.com
手机:15700769166
头像:stu3.png
性别:男
注册时间:1943-08-27 03:01:11
状态:不在线
编号:1003
用户名:abc4
密码:123456
邮箱:2109882888@qq.com
手机:15700769167
头像:stu4.png
性别:女
注册时间:1977-06-15 14:55:34
状态:在线
希望本文所述对大家php程序设计有所帮助。
希望与广大网友互动??
点此进行留言吧!