首页 > 开发 > PHP > 正文

PHP简单实现二维数组赋值与遍历功能示例

2024-05-04 22:45:07
字体:
来源:转载
供稿:网友

本文实例讲述了PHP简单实现二维数组赋值与遍历功能。分享给大家供大家参考,具体如下:

示例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 "编号:$loptop[$i][lid]"; //错误    //echo "编号:" . $loptop[$i]['lid']; //正确,但不推荐    $tmp = $loptop[$i];    echo "编号:$tmp[lid]<br/>";    echo "图片:$tmp[pic]<br/>";    echo "标题:$tmp[title]<br/>";    echo "价格:$tmp[price]<br/>";    echo "是否特价:$tmp[isOnSale]<br/>";    echo "上架时间:" . date("Y-m-d H:i:s",$tmp['shelfTime']) . "<br/>";  }?>

运行结果:

编号: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:

<?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;$i<count($stu);$i++){    $tmp = $stu[$i];    echo "编号:$tmp[sid]<br/>";    echo "用户名:$tmp[userName]<br/>";    echo "密码:$tmp[passWord]<br/>";    echo "邮箱:$tmp[email]<br/>";    echo "手机:$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 "状态:在线<br/>";    }    if($tmp['isOnline'] == 0){      echo "状态:不在线<br/>";    }  }?>            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表