需要在数组之间添加撇号


need to add apostrophes between arrays

我有一个音乐页面,除了标题还有cd。我也有列表:

"artist, upcoming artist, producer, featured artist and featured beatmakers"

所以每张音乐cd都写着:

"艺术家的标题(如果有),即将到来的艺术家(如果有),制作人(如有)、外聘艺人(如有)及外聘节拍师(如有)有的话)"

问题是……
如果有一个艺术家和一个即将到来的艺术家,或者一个外部艺术家和一个制作人或任何数组的组合,那么我需要在两个不同的数组或上面列出的数组的任何组合之间放一个空格和撇号.....

下面是我正在使用的代码:

<?php the_title(); ?> By <?php echo themex_artists(get_post_meta($post-ID,'freealbum_artists',true)); ?> 
<?php echo themex_artists(get_post_meta($post->ID,'freealbum_upcomingartists',true)); ?> 
<?php echo themex_artists(get_post_meta($post->ID,'freealbum_producers',true)); ?> 
<?php echo themex_artists(get_post_meta($post->ID,'freealbum_externalartists',true)); ?> 
<?php echo themex_artists(get_post_meta($post->ID,'freealbum_externalproducers',true)); ?> 

每个专辑都是不同的,所以名字并不总是在每个专辑的每个字段都显示....
我得想办法把它们分开。任何建议都会很好!

你需要把这个信息放到一个数组中首先过滤非空值最后用逗号分隔数组

$all_fields = [
    'freealbum_artists',
    'freealbum_upcomingartists',
    'freealbum_producers',
    'freealbum_externalartists',
    'freealbum_externalproducers'
];
$album_fields = [];
foreach ($all_fields as $field) {
    $album_fields[] = trim(themex_artists(get_post_meta($post->ID, $field, true)));
}
echo implode(', ', array_filter($album_fields, function ($val) {
    return $val ? true : false;
})), PHP_EOL;

如果我猜对了,试试这个(假设函数返回FALSE,如果没有什么要显示):

<?php the_title(); ?> By <?php echo themex_artists(get_post_meta($post-ID,'freealbum_artists',true)); 
echo themex_artists(get_post_meta($post->ID,'freealbum_upcomingartists',true));
if(themex_artists(get_post_meta($post->ID,'freealbum_upcomingartists',true)) && themex_artists(get_post_meta($post->ID,'freealbum_producers',true))) echo ", ";
echo themex_artists(get_post_meta($post->ID,'freealbum_producers',true));
if(themex_artists(get_post_meta($post->ID,'freealbum_producers',true)) && themex_artists(get_post_meta($post->ID,'freealbum_externalartists',true))) echo ", ";
echo themex_artists(get_post_meta($post->ID,'freealbum_externalartists',true));
if(themex_artists(get_post_meta($post->ID,'freealbum_externalartists',true)) && themex_artists(get_post_meta($post->ID,'freealbum_externalproducers',true))) echo ", ";
echo themex_artists(get_post_meta($post->ID,'freealbum_externalproducers',true)); ?>

你是这个意思吗?