使用implode()时复制最后一个值

时间:2017-12-07 00:43:54

标签: php duplicates implode

我遇到了php implode()函数的问题。奇怪的是,如果数组那么复制最后一个元素。这是一个更好解释的例子:

$arr = array('One', 'Two');

// Then...
$str = implode(' - ', $arr);

// Outputs: One - Two - Two

未在阵列中添加或删除任何元素。我试过array_unique,但只有独特的元素。

提前致谢。

被修改

以下是我的一些代码:

(isset($this->_title)) OR $this->_title = $this->__guess_title();
// Here $this->_title = 'Welcome'

(is_array($this->_title)) OR $this->_title = array($this->_title);
// Now $this->_title = array('Welcome')

$this->_title = apply_filters('the_title', $this->_title);
// The filter added site name, so the title array is
// array('Welcome', 'SiteName')

// $this->_title_sep = ' - ';
$this->_title = implode($this->_title_sep, $this->_title);

return $this->_title;
// Outputs: Welcome - SiteName - SiteName

以下是我添加过滤器的方法:

// $title is an array, so I add site_name to it and return it.
add_filter('the_title', function( $title ) {
    $title[] = option('site_name');
    return $title;
});

Here是数组,Here是在内爆之后(截图)。

1 个答案:

答案 0 :(得分:0)

<?php 
$arr = array('One', 'Two');
$str = implode(' - ', $arr);
echo $str;
?>

//Output is -> One - Two

The code works perfect and it doesn't display "One - Two - Two". I have tested it also and there's no duplicate for the last element.