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.

图片转代码服务由CSDN问答提供 功能建议

我有一个这样的数组。

  $ 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'; 
   \  n 
 

现在我要发布一个像这样的表

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

现在当我遍历数组时,我得到了这个

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

现在这不是我想要的,我知道简单的循环不会做我想要的。 有没有其他方法来实现这一点。

注意:给出的数据仅为样本数据。 我实际上在wordpress和wordpress中预定义的 template工作只是给我一个对象,所以我可以使用它。 在后端我已经尝试了所有甚至排序,但我得到了上面的数据。 我需要替代方案,所以我可以轻松更改,我不想修改默认文件。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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;
    
    点赞 打赏 评论