年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1845|回复: 1

二维数组的简单遍历

[复制链接]
  • TA的每日心情

    2024-10-15 10:05
  • 签到天数: 372 天

    [LV.9]以坛为家II

    发表于 2015-12-29 16:58:38 | 显示全部楼层 |阅读模式
    [PHP] 纯文本查看 复制代码
    <?php
    
    	$array1 = array("a","b","c","d","e","f","g","1");
    	$array2 = array("a","b","c","d","e","f","g","2");
    	$array3 = array("a","b","c","d","e","f","g","3");
    	$array4 = array("a","b","c","d","e","f","g","4");
    	$array5 = array("a","b","c","d","e","f","g","5");
    	$array6 = array("a","b","c","d","e","f","g","6");
    	$array7 = array("a","b","c","d","e","f","g","7");
    	$array = array($array1, $array2, $array3, $array4, $array5, $array6, $array7);
    	
    	echo '<table border="1" width="800" align="center">';
    	echo '<caption><h1>数组转换为表格</h1></caption>';
    	
    	foreach ($array as $k=>$row){
    		echo '<tr>';
    		foreach($row as $col){
    			echo "<td>$col</td>";
    		}
    		echo '</tr>';
    	}
    	echo '</table>';
    执行结果:
    屏幕快照 2015-12-29 下午4.58.19.png
  • TA的每日心情

    2024-10-15 10:05
  • 签到天数: 372 天

    [LV.9]以坛为家II

     楼主| 发表于 2015-12-29 17:46:20 | 显示全部楼层
    使用list()配合while、each()同样可以实现类似效果:
    [PHP] 纯文本查看 复制代码
    <?php
    
        $array1 = array("a","b","c","d","e","f","g","1");
        $array2 = array("a","b","c","d","e","f","g","2");
        $array3 = array("a","b","c","d","e","f","g","3");
        $array4 = array("a","b","c","d","e","f","g","4");
        $array5 = array("a","b","c","d","e","f","g","5");
        $array6 = array("a","b","c","d","e","f","g","6");
        $array7 = array("a","b","c","d","e","f","g","7");
        $array = array($array1, $array2, $array3, $array4, $array5, $array6, $array7);
        
        echo '<table border="1" width="800" align="center">';
        echo '<caption><h1>数组转换为表格</h1></caption>';
        while(list($key,$value)=each($array)){
    	    echo "<tr>";
    	    while(list($k,$v)=each($value)){
    		    echo "<td>$v</td>";
    	    }
    	    echo "</tr>";
        }
        echo "</table>";
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-12-22 17:06 , Processed in 0.054205 second(s), 21 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表