如何在foreach循环中向逗号分隔值添加文本?

时间:2017-07-03 06:19:07

标签: php yii

我有如下图所示的数据,我使用foreach循环来显示我的数据。

enter image description here

但我希望像他一样展示我的数据,我想在Ids的所有元素中添加“IC”..

enter image description here

这是我目前的代码..

prodebugenable -enable-all

我需要做些什么才能将“IC”添加到$ mod ['ids'],每行为<?php foreach ($model as $mod) { ?> <tr> <td><?= $mod['discount']; ?></td> <td><?= $mod['ids']; ?></td> </tr> <?php } ?>

2 个答案:

答案 0 :(得分:1)

当我评论使用 - 爆炸ID时,然后连接IC并再次推迟

$model = array(array('discount' => '2000', 'ids' => '100,1000,1000,1000'), array('discount' => '2001', 'ids' => '100,1000,1000,1000'),array('discount' => '2002', 'ids' => '100,1000,1000,1000'));
foreach ($model as $k=> $mod) {
    $mod_2 = preg_filter('/^/', 'IC', explode(',', $mod['ids']));
    echo $mod['discount'].'-'.implode(', ', $mod_2).PHP_EOL;
}

示例:https://3v4l.org/5I3l0

答案 1 :(得分:1)

通过foreach循环你可以做到!

<强> PHP

<?php
    $ids = "100,200,300,400,500,600";
    $ids_array = explode(",",$ids);
    $new_ids = array();
    foreach($ids_array as $values){
        $new_ids[] = "IC".$values;
    }
    echo implode(",",$new_ids);
?>

<强>输出

IC100,IC200,IC300,IC400,IC500,IC600
相关问题