Php 在输出中插入逗号分隔符

Php 在输出中插入逗号分隔符,php,cakephp,Php,Cakephp,如果有多个项目,我很难在每个项目后插入逗号分隔 我有所有应该有的值,只是缺少逗号 $result=$html->link($tags[$cv],数组('controller'=>'postTags','action'=>'view',$post_tags[$ck]),数组('title'=>'Vis artikler under'.$tags[$cv],'escape'=>false)); echo$result=substr($result,0,-2) 此输出不带逗号的正确链接:test1te

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

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

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

此输出不带逗号的正确链接:test1test2test3 if multiple=>应为;测试1,测试2,测试3

另外,如果只有1个item=>输出应该是test1(没有逗号)

所以,代码按原样输出正确的链接,但没有逗号!我不知道如何进行,有什么建议吗

尝试内爆(完整代码使用cake 1.3)

给我错误//

pr($post_标签)

公关(标签)


您应该使用符合您需要的:

用字符串连接数组元素

像这样使用它:

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

更新

// 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);
因此,使用您的示例代码:

         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;
            }
        }

这就是你要找的:

$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

@Tom“不工作”是什么意思?如果执行
print\r($result)
您会得到什么?它不是数组,不确定如何继续,请参阅主主题以获取完整代码。这是因为结果是字符串,它需要是数组。是的。。我想。。。有什么建议吗?请参阅整个代码的主主题。请参阅我的更新。。。如果我发布的示例不起作用,你能发布一个
$post\u标签结构的示例吗。。。而且,在我看来,你的链接助手的参数可能有问题。。。你在使用什么框架/应用程序?我试过了,但不起作用。。我正在使用cakephp。正如我在主要主题中提到的,它输出正确的类别和链接,我只是缺少逗号=>testCat1,testCat2++。它输出正确的链接,如:testCat1testCat2+++对,但正确的方法是在链接中使用类别之前先对其进行维护。因此,在将它们编译成字符串
testCat1testCat2
之前,您需要使用它们,而不是在它们处于静止状态、数组或记录集时尝试在事后进行字符串替换。所以我再问一次,你从哪里获得
$post_标签
数据,这个数组目前看起来像什么?它可能很简单,只需将
getFormattedTags
方法添加到
Post
模型中即可。
// 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;
            }
        }
$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
                          )
                      );