跳过php if语句中的最后一个数据

时间:2014-02-20 10:49:06

标签: php skip

我想打印并在它们之间添加一个字符(例如*),如果id是特定的(例如3),但我不希望在最后一个数据之后添加它。

我有这段代码:

<?php 
foreach ($attribute_group['attribute'] as $attribute) {

if ($attribute_group['attribute_group_id'] == '3') {

    echo implode(' * ', $attribute['name'] . $attribute['text']);
    }

    else

    echo ($attribute['name'] . $attribute['text']);
    }

&GT;

现在输出如下:

Attribute 1 * Attribute 2 * Attribute 3 *

我想要它:

Attribute 1 * Attribute 2 * Attribute 3

2 个答案:

答案 0 :(得分:0)

删除最后一个字符:

$string = "Attribute 1 * Attribute 2 * Attribute 3 *";
echo rtrim($string, "*");

答案 1 :(得分:0)

您也可以rtrim()尝试这样做:
演示:https://eval.in/103723

$data =  array(
        '1' => 'Attribute 1',
        '2' => 'Attribute 2',
        '3' => 'Attribute 3',
        '4' => 'Attribute 4',
        '5' => 'Attribute 5'
    );

    $strOutput="";
    foreach ($data as $d){
     $strOutput .= $d." * ";

    } 

    echo rtrim($strOutput," * ");