doutan1857
2018-02-06 09:11 阅读 201

删除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 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 ","

    点赞 评论 复制链接分享
  • dongyan2267 dongyan2267 2018-02-06 09:18

    if you just need to remove the last char you can use substr

      $my_result =substr( $actors, 0, -1); 
    
    点赞 评论 复制链接分享
  • duanjieyi6582 duanjieyi6582 2018-02-06 09:19

    Create an array to convert in JSON like below

    $actors = array();
    foreach ($actors_name as $key => $actor) {
        $actors[] = [
            "type" => "Person",
            "name" => $actor,
            "url" => $urls_actors[$key],
            "sameAs" => $urls_actors[$key],
            ];
    }
    

    And use json_encode to print array as json

    echo json_encode(["actor"=>$actors]);
    
    点赞 评论 复制链接分享
  • doushan2311 doushan2311 2018-02-06 09:25

    you should try this.

     foreach ($actors_name as $key => $actor) {     
       if($key<(count($actors_name)-1)){
            $actors .= '
            {
                "@type": "Person",
                "name": "'.$actor.'",
                "url": "'.$urls_actors[$key].'",
                "sameAs": "'.$urls_actors[$key].'"
            },';
       }else{
            $actors .= '
            {
                "@type": "Person",
                "name": "'.$actor.'",
                "url": "'.$urls_actors[$key].'",
                "sameAs": "'.$urls_actors[$key].'"
        }';
       }
    
     }
    
    点赞 评论 复制链接分享

相关推荐