doutan1857 2018-02-06 09:11
浏览 515
已采纳

删除JSON输出列表中的最后一个逗号

I need to remove the last comma of a list of actors in order to parse Google JSON Data Structural Test, i tried several methods with no result, only the last comma has to be removed from output to get no errors from Google. This is the code:

    $urls_actors = amy_movie_get_movie_taxomony3( $post->ID, 'amy_actor' );
$urls_actors = explode("," ,$urls_actors);

$actors_name = amy_movie_get_movie_taxomony2( $post->ID, 'amy_actor' );
$actors_name = explode("," ,$actors_name);

foreach ($actors_name as $key => $actor) {
$actors .= '
        {
            "@type": "Person",
            "name": "'.$actor.'",
            "url": "'.$urls_actors[$key].'",
            "sameAs": "'.$urls_actors[$key].'"
        },

    ';
}

<script type="application/ld+json">

    "actor": [
<?php echo $actors;?>],

</script>

And this is the output with the actors list in the script:

    "actor": [

        {
            "@type": "Person",
            "name": "Apesanahkwat",
            "url": "",
            "sameAs": ""
        },


        {
            "@type": "Person",
            "name": " Carol Pounder",
            "url": "",
            "sameAs": ""
        },


        {
            "@type": "Person",
            "name": " Christine Kaufmann",
            "url": "",
            "sameAs": ""
        },


        {
            "@type": "Person",
            "name": " Darron Flagg",
            "url": "",
            "sameAs": ""
        },


        {
            "@type": "Person",
            "name": " G. Smokey Campbell",
            "url": "",
            "sameAs": ""
        },  //I need to remove this comma!
],

Thanks for helping out!

  • 写回答

4条回答 默认 最新

  • douchenzhan3050 2018-02-06 09:16
    关注

    Use Below code it will work for you.

    $urls_actors = amy_movie_get_movie_taxomony3( $post->ID, 'amy_actor' );
    $urls_actors = explode("," ,$urls_actors);
    
    $actors_name = amy_movie_get_movie_taxomony2( $post->ID, 'amy_actor' );
    $actors_name = explode("," ,$actors_name);
    
    foreach ($actors_name as $key => $actor) {
    $actors_list[] = '
            {
                "@type": "Person",
                "name": "'.$actor.'",
                "url": "'.$urls_actors[$key].'",
                "sameAs": "'.$urls_actors[$key].'"
            }';
    }
    
    <script type="application/ld+json">
    
        "actor": [
    <?php echo implode(',',$actors_list);?>],
    
    </script>
    

    I just take values in array and at last. I implode it with ","

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Qt安装后运行不了,这是我电脑的问题吗
  • ¥15 数据量少可以用MK趋势分析吗
  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法