我有 $product.features
PrestaShop 1.7中的数组。
这里是数组中的样本数据。$product.features.name: $product.features.value
width: 100 m
method: Nail Down
method: Main Floor
Warranty: 25 years
Color: Red
Color: Blue
我想打印上述数据作为
Width: 100m
method: Nail Down, Main Floor
Warranty: 25 years
Color: Red, Blue
以下是我的smarty代码
{foreach from=$product.features item=feature}
<div class="col-md-3 col-sm-6 col-xs-12">
<div class="name">{$feature.name}</div>
</div>
<div class="col-md-3 col-sm-6 col-xs-12">
<div class="value flex_child">{$feature.value}</div>
</div>
{/foreach}
试试这个。
{foreach from=$product.features item=feature}
<div class="col-xs-12">
<div class="name">{$feature.name}: {$feature.value}</div>
</div>
{/foreach}
一种选择是为循环键和值准备特征数组。
例如,使用 缩减数组 和 爆炸 返回一个数组,其中键是在 :
和值是内爆的结果,用 ,
作为定界符。
$features = [
"width with: 100 m",
"method: Nail Down",
"method: Main Floor",
"Warranty: 25 years",
"Color: Red",
"Color: Blue"
];
$features = array_reduce($features, function($carry, $item){
$parts = explode(":", $item);
$carry[$parts[0]][] = $parts[1];
return $carry;
});
foreach ($features as $key => $value) {
echo "$key: " . implode(',', $value) . PHP_EOL;
}
输出
width: 100 m
method: Nail Down, Main Floor
Warranty: 25 years
Color: Red, Blue
看一个 Php演示