如果有多个项目,我很难在每个项目之后插入逗号分隔。
我有他们应该的所有价值观,只缺少逗号。
$ result = $ html-> link($ tags [$ cv],array('controller'=>'postTags','action'=>'view',$ post_tags [$ ck]), array('title'=>'Vis artikler under'。$ tags [$ cv],'escape'=> false)); echo $ result = substr($ result,0,-2);
此输出正确链接而不使用逗号:test1test2test3如果multiple =>应该; test1,test2,test3
此外,如果只有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($标签);
Array
(
[1] => Tag1
[2] => Tag2
[3] => Tag3
[4] => Tag4
)
答案 0 :(得分:2)
<强>更新强>
// 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);
如果您将类别作为数组,请使用implode:
$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;
}
}
答案 1 :(得分:1)
答案 2 :(得分:0)
这就是你要找的东西:
$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;
。