显示两列表上的关联数组中的项目

时间:2013-11-02 19:36:23

标签: php html arrays html-table associative

我正在尝试使用此关联数组将关联数组中的项显示为两列表 foreach($this->inventory as $ID => $Info)

"<table width ='100%'>\n"; 
 foreach($this->inventory as $ID => $Info) {
    echo"<tr>";   
    echo"<td>".$Info['name'] <br>$Info['description']." </td>";
    echo"<td>".$Info['name'] <br>$Info['description']."</td>"; 
    echo"</tr>";
}
</table>

问题是每行显示两次项目。 我希望它显示如下:

------------------------------------------------------------------
|Name: shoes                      |Name: bag
|Description: nike                |Description: swing bag
 -----------------------------------------------------------------
|Name: socks                      |Name: ear phones
|Description: black and white     |Description: beats
 -----------------------------------------------------------------
|Name: earrings                   |Name: phone
|Description: diamond  studs      |Description: blackberry  

但是我得到了这个:

------------------------------------------------------------------
|Name: shoes                      | Name: shoes
|Description: nike                | Description: nike
 -----------------------------------------------------------------
|Name: bag                        | Name: bag 
|Description: swing bag           |Description: swing bag 
 -----------------------------------------------------------------
|Name: ear phones                 |Name: ear phones
|Description: beats               |Description: beats  

1 个答案:

答案 0 :(得分:1)

我在这个小提琴http://jsfiddle.net/hnek2/

中为这个伪表创建了CSS

现在你可以使用这个PHP:

echo('<div class="tbl">');
foreach($this->inventory as $ID => $Info) {
    echo('<div class="block">');   
    echo('<p>Name: ' . $Info['name'] . '</p><p>Description: ' . $Info['description'] . '</p>');
    echo('</div>');
}
echo('</div>');