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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算