获取数组的最后一个元素以从中删除逗号
Get the last element of an array to remove comma from it
我有一个简单的函数,显示值后跟一个逗号。
if ( $hasCoAuthors ) :
foreach ( $co_authors as $co_author ) :
$coAuthorDisplayName = $co_author->display_name;
$coAuthor = '<span class="co-author">' . $coAuthorDisplayName . '</span>, ';
echo $coAuthor;
endforeach;
endif;
如何获取最后一个值以从中删除逗号?
我添加了这个功能:
function endKey( $array ) {
end( $array );
return key( $array );
}
然后我在我的函数中添加了这个:
if ( endKey( $co_authors ) ) :
$coAuthor = '<span class="co-author">' . $coAuthorDisplayName . '</span>';
endif;
但这不起作用,它会从所有值中删除逗号。
如果键是数字(0、1、2 等),您可以简单地使用此代码将 ,
添加到 2nd 和其他。
if ( $hasCoAuthors ) :
foreach ( $co_authors as $key => $co_author ) :
$coAuthor = '';
if ($key > 0) {
$coAuthor = ', ';
}
$coAuthorDisplayName = $co_author->display_name;
$coAuthor .= '<span class="co-author">' . $coAuthorDisplayName . '</span>, ';
echo $coAuthor;
endforeach;
endif;
第二种方法是将值放入array
并使用implode
。
if ( $hasCoAuthors ) :
$coAuthors = [];
foreach ( $co_authors as $key => $co_author ) :
$coAuthorDisplayName = $co_author->display_name;
$coAuthors[] = '<span class="co-author">' . $coAuthorDisplayName . '</span>';
endforeach;
echo implode(', ', $coAuthors);
endif;
你每次都在打印值,这是不必要的,将它们连接成一个字符串变量,然后在循环结束后打印它,方法是使用 rtrim()
删除最后一个逗号。
您需要像下面这样更正循环代码:
if ( $hasCoAuthors ) :
$coAuthor = '';
foreach ( $co_authors as $co_author ) :
$coAuthor .= '<span class="co-author">' . $co_author->display_name . '</span>, ';
endforeach;
echo rtrim($coAuthor, ',');
endif;
在字符串中构建 html,然后使用 trim()
trim 最后一个逗号,然后在循环完成时回显一次。
if ( $hasCoAuthors ) :
$coAuthor = '';
foreach ( $co_authors as $co_author ) :
$coAuthor .= '<span class="co-author">' . $co_author->display_name . '</span>, ';
endforeach;
echo rtrim($coAuthor, ',');
endif;
或者如@CBroe 所建议的那样
if ( $hasCoAuthors ) :
$coAuthor = '';
foreach ( $co_authors as $k => $co_author ) :
if ($k > 0 ) {
$coAuthor .= ','
}
$coAuthor .= '<span class="co-author">' . $co_author->display_name . '</span>';
endforeach;
echo $coAuthor;
endif;
我有一个简单的函数,显示值后跟一个逗号。
if ( $hasCoAuthors ) :
foreach ( $co_authors as $co_author ) :
$coAuthorDisplayName = $co_author->display_name;
$coAuthor = '<span class="co-author">' . $coAuthorDisplayName . '</span>, ';
echo $coAuthor;
endforeach;
endif;
如何获取最后一个值以从中删除逗号?
我添加了这个功能:
function endKey( $array ) {
end( $array );
return key( $array );
}
然后我在我的函数中添加了这个:
if ( endKey( $co_authors ) ) :
$coAuthor = '<span class="co-author">' . $coAuthorDisplayName . '</span>';
endif;
但这不起作用,它会从所有值中删除逗号。
如果键是数字(0、1、2 等),您可以简单地使用此代码将 ,
添加到 2nd 和其他。
if ( $hasCoAuthors ) :
foreach ( $co_authors as $key => $co_author ) :
$coAuthor = '';
if ($key > 0) {
$coAuthor = ', ';
}
$coAuthorDisplayName = $co_author->display_name;
$coAuthor .= '<span class="co-author">' . $coAuthorDisplayName . '</span>, ';
echo $coAuthor;
endforeach;
endif;
第二种方法是将值放入array
并使用implode
。
if ( $hasCoAuthors ) :
$coAuthors = [];
foreach ( $co_authors as $key => $co_author ) :
$coAuthorDisplayName = $co_author->display_name;
$coAuthors[] = '<span class="co-author">' . $coAuthorDisplayName . '</span>';
endforeach;
echo implode(', ', $coAuthors);
endif;
你每次都在打印值,这是不必要的,将它们连接成一个字符串变量,然后在循环结束后打印它,方法是使用 rtrim()
删除最后一个逗号。
您需要像下面这样更正循环代码:
if ( $hasCoAuthors ) :
$coAuthor = '';
foreach ( $co_authors as $co_author ) :
$coAuthor .= '<span class="co-author">' . $co_author->display_name . '</span>, ';
endforeach;
echo rtrim($coAuthor, ',');
endif;
在字符串中构建 html,然后使用 trim()
trim 最后一个逗号,然后在循环完成时回显一次。
if ( $hasCoAuthors ) :
$coAuthor = '';
foreach ( $co_authors as $co_author ) :
$coAuthor .= '<span class="co-author">' . $co_author->display_name . '</span>, ';
endforeach;
echo rtrim($coAuthor, ',');
endif;
或者如@CBroe 所建议的那样
if ( $hasCoAuthors ) :
$coAuthor = '';
foreach ( $co_authors as $k => $co_author ) :
if ($k > 0 ) {
$coAuthor .= ','
}
$coAuthor .= '<span class="co-author">' . $co_author->display_name . '</span>';
endforeach;
echo $coAuthor;
endif;