在输出中插入逗号分隔


inserting comma separated to output

如果有多个项目,我很难在每个项目后面插入逗号分隔的内容。

我有所有应该有的值,只是缺少逗号。

$result=$html->link($tags[$cv],array('controller'=>'postTags','action'=>'view',$post_tags[$ck]),array)('title'=>'Vis-artikler-under'.$tags[%cv],'scape'=>false);echo$result=substr($result,0,-2);

此输出更正了不带逗号的链接:test1test2test3如果multiple=>应为;测试1、测试2、测试3

此外,如果只有1项=>输出应为test1(无逗号)。

所以,按原样编码,输出正确的链接,但没有逗号!我不知道该怎么办,有什么建议吗?

尝试内爆(完整代码使用蛋糕1.3);

$ci = 0;
    $post_tags = explode(",", $content['Post']['tag_id']);
        if(!empty($post_tags)){
            foreach($post_tags as $ck => $cv) { 
                if(isset($tags[$cv])){
                    $ci = $ci+1;
                    $result = $html->link($tags[$cv], array('controller'=>'postTags','action' => 'view', $post_tags[$ck]), array('title'=>'Vis artikler under '.$tags[$cv],'escape' => false));
                    //pr($result);
                    $commaSeparated = implode(',',$result);
                    echo $commaSeparated;
                }
            }
        } else {
            echo '';
        }

给我一个错误..;//

pr($post_tags);

Array
(
    [0] => 3
    [1] => 1
    [2] => 2
)

pr($tags);

Array
(
    [1] => Tag1
    [2] => Tag2
    [3] => Tag3
    [4] => Tag4
)

更新

// get only the tags assigned to the post
$postTagKeys = array_flip($post_tags);
$tags = array_intersect_key($tags, $postTagKeys);
// ok lets make the links:
$tagLinks = array();
foreach($tags as $tagId => $tagName) {
   $tagLinks[] = $html->link(
       $tagName,
       array('controller'=>'postTags','action' => 'view', $tagId),
       array('title'=>'Vis artikler under '.$tagName,'escape' => false)
   );
}
//$tagLinks is now an array of html <a> tags linking to individual tags
// so to ouput the list we do
echo implode(', ', $tagLinks);

如果你有作为一个数组的类别,只需使用内爆:

$cats = array('test1','test2','test3');
$cats2 = array('test1');
echo implode(', ',$cats);
echo implode(', ',$cats2);

因此,使用您的示例代码:

         foreach($post_tags as $ck => $cv) { 
            if(isset($tags[$cv])){
                $ci = $ci+1;
                $taglist = implode(', ', $tags[$cv]);
                $result = $html->link($taglist, array(
                  'controller'=>'postTags',
                  'action' => 'view', 
                  $post_tags[$ck]  // are you sure you want to pass the array here and not just the array key?
                ), array(
                  'title'=>'Vis artikler under '.$taglist,
                  'escape' => false)
                );
                echo $result;
            }
        }

您应该使用内爆(),它正是您所需要的:

用字符串连接数组元素

像这样使用:

$commaSeparated = implode(',', $array);

这就是您想要的:

$result = $html->link($tags[$cv] . ((count($tags) > 1 && $ci > 0 && count($tags) != $ci) ? ', ' : ''), array(
                              'controller' => 'postTags',
                              'action' => 'view',
                              $post_tags[$ck]
                          ), array(
                              'title' => 'Vis artikler under ' . $tags[$cv],
                              'escape' => false
                          )
                      );

顺便说一下,我们只使用$ci++;而不是$ci = $ci+1;