首頁  >  文章  >  後端開發  >  探讨php中遍历二维数组的几种方法详解_PHP教程

探讨php中遍历二维数组的几种方法详解_PHP教程

WBOY
WBOY原創
2016-07-21 15:08:241208瀏覽

复制代码 代码如下:

//使用for循环遍历
$arr2=array(array("张三","20","男"),array("李四","25","男"),array("王五","19","女"),array("赵六","25","女"));
echo "& gt;";
for($i=0;$iecho "";
for($j=0;$j   echo "";
}
echo "";
echo "
";
}
echo "
姓名 年龄性别
";
   echo $arr2[$i][$j];
   echo "
";
?>

//使用foreach遍历
复制代码 代码如下:

$arr = array('one'=>array('name'=>'张三','age'=>'23','sex'=>'男'),
    'two'=>array('name'=>'李四','age'=>'43','sex'=>'女'),
    'three'=>array('name'=>'王五','age'=>'32','sex'=>'男'),
    'four'=>array('name'=>'赵六','age'=>'12','sex'=>'女'));

foreach($arr as $k=>$val){
   echo $val['name'].$val['age'].$val['sex']."
";
}
echo "

";
?>


复制代码 代码如下:

$arr = array('one'=>array('name'=>'张三','age'=>'23','sex'=>'男'),
    'two'=>array('name'=>'李四','age'=>'43','sex'=>'女'),
    'three'=>array('name'=>'王五','age'=>'32','sex'=>'男'),
    'four'=>array('name'=>'赵六','age'=>'12','sex'=>'女'));
foreach($arr as $key=>$value){
foreach($value as $key2=>$value2){
   echo $value2;
}
echo "
";
}

?>


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/327480.htmlTechArticle复制代码 代码如下: ?php //使用for循环遍历 $arr2=array(array("张三","20","男"),array("李四","25","男"),array("王五","19","女"),array("赵六","25","女")); ec...
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn