如何删除尾随逗号 [英] How to Remove Trailing Comma

查看:68
本文介绍了如何删除尾随逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的php语句中删除结尾的逗号

I am trying to remove the trailing comma from my php statement

<?php foreach( $speaker_posts as $sp ): ?>
{
    "@type" : "person",
    "name" : "<?php the_field('name_title', $sp->ID); ?>",
    "sameAs" : "<?php echo post_permalink( $sp->ID ); ?>"
},
<?php endforeach; ?>


推荐答案

由于您显然要输出JSON,因此请使用PHP来

Since you're apparently outputting JSON, use PHP to do it.

<?php

$json = [];

foreach($speaker_posts as $sp) {
    $json[] = [
        '@type' => 'person',
        'name' => get_field('name_title', $sp->ID),
        'sameAs' => post_permalink( $sp->ID ),
    ];
}

print json_encode($json);

?>

旁注:这将使您避免在字段/永久链接中使用诸如引号/撇号之类的不安全字符内容。

Side note: this will save you from potentially unsafe characters like quotation marks / apostrophes in the field/permalink contents.

这篇关于如何删除尾随逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆