在foreach返回值之间添加分隔符

时间:2019-05-16 09:46:13

标签: php laravel laravel-blade

我有以下foreach返回值:

@foreach($content['order'] as $products)
    <tr>
        <td><span>{{ $products->Type }} {{ $products->Nom }} {{ $products->{'# Personnes'} }} {{ $products->Inscription }} {{ $products->{'Décoration Chocolat et fruits'} }} {{ $products->{'Décoration petites fleurs'} }} {{ $products->Décoration }} {{ $products->{'Nombre de sandwiches'} }} {{ $products->Poids }} {{ $products->{'Assortiment 1'} }} {{ $products->{'Assortiment 2'} }} {{ $products->{'Assortiment 3'} }} {{ $products->{'Assortiment 4'} }} {{ $products->{'Couleur Ruban'} }} {{ number_format($products->Prix, 2) }} {{ number_format($products->Supplément, 2) }} {{ number_format($products->{'Supplément décoration'}, 2) }} {{ number_format($products->Total, 2) }}</td>
    </tr>
@endforeach

我想在每个返回值之间添加一个分隔符(最后一个除外)。

我添加了

.' | '

{{ $products->Type.' | ' }} {{ $products->Nom.' | ' }} {{ $products->{'# Personnes'}.' | ' }} {{ $products->Inscription.' | ' }} {{ $products->{'Décoration Chocolat et fruits'}.' | ' }} {{ $products->{'Décoration petites fleurs'}.' | ' }} {{ $products->Décoration.' | ' }} {{ $products->{'Nombre de sandwiches'}.' | ' }} {{ $products->Poids.' | ' }} {{ $products->{'Assortiment 1'}.' | ' }} {{ $products->{'Assortiment 2'}.' | ' }} {{ $products->{'Assortiment 3'}.' | ' }} {{ $products->{'Assortiment 4'}.' | ' }} {{ $products->{'Couleur Ruban'}.' | ' }} {{ number_format($products->Prix, 2).' | ' }} {{ number_format($products->Supplément, 2).' | ' }} {{ number_format($products->{'Supplément décoration'}, 2).' | ' }} {{ number_format($products->Total, 2) }}

我遇到的问题是即使没有返回值也要添加分隔符

Pie | Chocolate | 2-3 servings | Text | Decoration chocolate | Decoration flowers | | | | | | | | | 19.00 | 5.00 | 0.00 | 24.00

仅当返回值时,如何打印分隔符。

谢谢

4 个答案:

答案 0 :(得分:1)

@if(isset($products->Type)) {{ $products->Type. '|' }}  @endif 

这是第一行处理方式的示例。您检查是否存在,以及是否echo在其上连接分隔符的结果。

小东西,请注意您的span标签,您没有在提供的代码中关闭它。

答案 1 :(得分:0)

您可以使用三元运算符作为简写,

{{ !empty($products->Type) ? $products->Type.' | ': '' }}

对所有字段执行此操作

答案 2 :(得分:0)

有多种解决方法,但是一种非常动态的方法是将所有值都提取到数组中,为非空值过滤并内爆。

我们将使用标准的PHP,您可以在刀片服务器中进行操作。

<?php
foreach($content['order'] as $products) {
    $values = [];
    $values[] = $products->Type;
    $values[] = $products->Nom;
    $values[] = $products->{'# Personnes'};
    $values[] = $products->Inscription;
    $values[] = $products->{'Décoration Chocolat et fruits'};
    $values[] = $products->{'Décoration petites fleurs'};
    $values[] = $products->Décoration;
    $values[] = $products->{'Nombre de sandwiches'};
    $values[] = $products->Poids;
    $values[] = $products->{'Assortiment 1'};
    $values[] = $products->{'Assortiment 2'};
    $values[] = $products->{'Assortiment 3'};
    $values[] = $products->{'Assortiment 4'};
    $values[] = $products->{'Couleur Ruban'};
    $values[] = number_format($products->Prix, 2);
    $values[] = number_format($products->Supplément, 2);
    $values[] = number_format($products->{'Supplément décoration'}, 2);
    $values[] = number_format($products->Total, 2);

    echo "<td>".implode(" | ", array_filter($values))."</td>";
}
?>

答案 3 :(得分:0)

变成数组,过滤空值并使用“ |”字符串内爆

 {{join("|", array_filter([
 $products->Type ,
 $products->Nom ,
 $products->{'# Personnes'} ,
 $products->Inscription ,
 $products->{'Décoration Chocolat et fruits'} ,
 $products->{'Décoration petites fleurs'} ,
 $products->Décoration ,
 $products->{'Nombre de sandwiches'} ,
 $products->Poids ,
 $products->{'Assortiment 1'} ,
 $products->{'Assortiment 2'} ,
 $products->{'Assortiment 3'} ,
 $products->{'Assortiment 4'} ,
 $products->{'Couleur Ruban'} ,
 number_format($products->Prix, 2) ,
 number_format($products->Supplément, 2) ,
 number_format($products->{'Supplément décoration'}, 2) ,
 number_format($products->Total, 2)
 ]))}}