Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用于添加额外逗号的串联_Php_Arrays_Concatenation - Fatal编程技术网

Php 用于添加额外逗号的串联

Php 用于添加额外逗号的串联,php,arrays,concatenation,Php,Arrays,Concatenation,当一篇文章中有不止一位作者,但出于某种原因,它会添加额外的逗号时,我会尝试使用concat来命名作者。这是我使用的代码: $get_coauthors = get_coauthors(); $count = count( $get_coauthors ); if ( $count > 1 ) { for ( $i = 0; $i <= $count; $i++ ) { $name .= $get_coauthor

当一篇文章中有不止一位作者,但出于某种原因,它会添加额外的逗号时,我会尝试使用concat来命名作者。这是我使用的代码:

        $get_coauthors = get_coauthors();
    $count = count( $get_coauthors );
    if ( $count > 1 ) {
        for ( $i = 0; $i <= $count; $i++ ) {
            $name .= $get_coauthors[ $i ]->data->display_name . ' ,';
        }
    } else {
        $name = $get_coauthors[0]->data->display_name;
    }
    error_log(print_r($name,true));
$get_coauthors=get_coauthors();
$count=count($get\u合著者);
如果($count>1){
对于($i=0;$i数据->显示_名称。“,”;
}
}否则{
$name=$get\u共同作者[0]->data->display\u name;
}
错误日志(打印($name,true));
我的打印结果返回
user1、user2、


想知道为什么吗?

不需要
count()
if
语句。这可能更简单:

//If you already have a $name
$names[] = $name;

foreach(get_coauthors() as $author) {
    $names[] = $author->data->display_name;
}
$names = implode(', ', $names);
若要清除任何空容器,请执行以下操作:

$names = implode(', ', array_filter($names));

无需使用
count()
if
语句。这可能更简单:

//If you already have a $name
$names[] = $name;

foreach(get_coauthors() as $author) {
    $names[] = $author->data->display_name;
}
$names = implode(', ', $names);
若要清除任何空容器,请执行以下操作:

$names = implode(', ', array_filter($names));
您可以使用
rtrim()
从姓名列表中删除最后一个

差不多

$srting = 'a,b,c,d,e,';
$result_output = rtrim($srting);
echo $result_output;
然后输出:
a、b、c、d、e

因此,您的代码将是

$names = '';
foreach(get_coauthors() as $author) {
    $names .= $author->data->display_name. ' ,';
}
$result = rtrim($names,",");
或者获取数组中的所有名称,然后将其内爆,然后您将得到类似user1、users2等的结果

$names = array();
foreach(get_coauthors() as $author) {
    $names = $author->data->display_name;
}
$names = implode(', ', $names);
有关阵列内爆的更多帮助

您可以使用
rtrim()
从姓名列表中删除最后一个

差不多

$srting = 'a,b,c,d,e,';
$result_output = rtrim($srting);
echo $result_output;
然后输出:
a、b、c、d、e

因此,您的代码将是

$names = '';
foreach(get_coauthors() as $author) {
    $names .= $author->data->display_name. ' ,';
}
$result = rtrim($names,",");
或者获取数组中的所有名称,然后将其内爆,然后您将得到类似user1、users2等的结果

$names = array();
foreach(get_coauthors() as $author) {
    $names = $author->data->display_name;
}
$names = implode(', ', $names);
有关阵列内爆的更多帮助


请发布
print\r($get\u合著者)
如果没有看到您的数据,就无法确定结果,但我猜其中一位作者的名字是空的。您的循环比数组大,并且运行到一个不存在的事件上($I=0;$I请发布
print\r($get\u合著者)的循环
如果看不到您的数据就无法确定,但我猜其中一位作者的名字是空的。您的循环比数组大,并且运行到($I=0;$I)中不存在的事件