Php 如何将数组输出到行中有2列的表中
下面是我的代码Php 如何将数组输出到行中有2列的表中,php,Php,下面是我的代码 $itemSpec = Array ( [0] => Brand:Adidas [1] => Style:Snowsuits & Bibs [2] => Country:China [3] => Material:Cotton Blend [4] => Sport:Bowling [5] => Sleeve length:Short Sleeve [6] => Theme
$itemSpec = Array
(
[0] => Brand:Adidas
[1] => Style:Snowsuits & Bibs
[2] => Country:China
[3] => Material:Cotton Blend
[4] => Sport:Bowling
[5] => Sleeve length:Short Sleeve
[6] => Theme:Liverpool
)
上述数组值是项目规格的标题和值的组合,由以下内容分隔:
foreach ($itemSpec as $v){
$allItemSpec = explode(':', $v);
if($allItemSpec[0] == "Country"){
$allItemSpec[0] = "Country/Region of
Manufacture";
}
$allItemSpec = "<tr>"
"<td>$allItemSpec[0]:</td>
<td>$allItemSpec[1]</td>
</tr>";
echo $allItemSpec;
}
foreach($itemSpec为$v){
$allItemSpec=爆炸(“:”,$v);
如果($allItemSpec[0]=“国家”){
$allItemSpec[0]=“所属国家/地区”
制造”;
}
$allItemSpec=“”
“$allItemSpec[0]:
$allItemSpec[1]
";
echo$allItemSpec;
}
下面是我想要实现的形象
我想要达到的目标
下面是我目前的结果
我只希望tr中有4个td,如果我从代码中删除2个td,我得到的tr中只有2个td,这不是我想要的结果,我希望有人能帮助我,并提前感谢。
<style type="text/css">
.container { width: 1000px; float: left; }
.container .item { width: 50%; float: left; height: 50px; }
</style>
<?php
$itemSpec = Array
(
0 => "Brand:Adidas",
1 => "Style:Snowsuits & Bibs",
2 => "Country:China",
3 => "Material:Cotton Blend",
4 => "Sport:Bowling",
5 => "Sleeve length:Short Sleeve",
6 => "Theme:Liverpool"
);
echo '<div class="container">';
foreach ( $itemSpec as $v ) {
$allItemSpec = explode(':', $v);
if( $allItemSpec[0] == "Country" ) {
$allItemSpec[0] = "Country/Region of Manufacture";
}
echo '<div class="item">' .$allItemSpec[0]. ":" .$allItemSpec[1] . '</div>';
}
echo '</div>';
?>
.container{width:1000px;float:left;}
.container.item{宽度:50%;浮动:左侧;高度:50px;}
当前结果不是代码所做的。也可以使用['key'=>'value']而不是分解。请发送实际的当前代码谢谢您的回复我将编辑并详细解释@Friedrich Roell我编辑了我的问题,并更改了第二个图像,这是我从上面的代码中得到的实际结果,数组$itemspec正如您在上面看到的,是一个索引数组,值是一个由:符号分隔的字符串,现在我想把:号左边的字符串作为标题,右边的字符串作为值,所以我使用explode,这给了我想要的结果,现在我需要在一个表中输出结果,我只需要在一个tr上输出2,所以如果数组中有7,这意味着我需要4个tr,其中一个只有on td,但在我的结果中,我得到了7排感谢您的重播,我得到了与您完全相同的结果,因此,也许我需要的是css样式,例如品牌:阿迪达斯和样式:滑雪服和围兜在同一排,请您帮我处理css,谢谢