doumao1047 2013-06-29 12:15
浏览 43
已采纳

如何以表格格式显示在php中彼此之前具有字段的数组

I have an array like this.

$array[0]   =   'email1';
$array[1]   =   'email2';
$array[2]   =   'email3';
$array[3]   =   'mobile1';
$array[4]   =   'mobile2';
$array[5]   =   'mobile3';
$array[6]   =   'email1_c';
$array[7]   =   'email2_c';
$array[8]   =   'email3_c';
$array[9]   =   'mobile1_c';
$array[10]  =   'mobile2_c';
$array[11]  =   'mobile3_c';

Now i want to dispay a table like this

--------------------------------
email1      |       email1_c
email2      |       email2_c
email3      |       email3_c
mobile1     |       mobile1_c
mobile2     |       mobile2_c
mobile3     |       mobile3_c

Now when i loop through the array i get this

--------------------------------
email1      |       email2
email3      |       mobile1
mobile2     |       mobile3
email1_c    |       email2_c
email2_c    |       mobile1_c
mobile1_c   |       mobile3_c

Now this is not what i want and i know simple looping wont do what i want. So is there any alternative way to achieve this.

Note : The data given is only sample data. I am actually working in a pre-defined template in wordpress and wordpress simply throw me an object so i can use it. In the backend i have tried everything even sorting but i get the data like above. I need alternative so i can change easily and i dont want to modify default files.

  • 写回答

1条回答 默认 最新

  • dpklt4291 2013-06-29 12:22
    关注

    It appears that it depends on an offset value. As you have it shown that value would be six:

    $html = "<table>
    ";
    $offset = 6;
    for ($i=0; $i<=5; $i++){
        $html .= "<tr><td>".$array[$i]."</td><td>".$array[$i + $offset]."</td></tr>
    ";
    }
    $html .= "</table>
    ";
    

    Depending on what you need you might need to dynamically calculate the offset. For example:

    $offset = count($array)/2;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝