News新闻

业界新闻动态、技术前沿
Who are we?

您的位置:首页      乐道系统FAQ      PHP简单实现二维数组赋值与遍历功能示例

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

发布日期:2017-10-19 00:00:00 108

本文实例讲述了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/>";
  }
?>