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

时间:2018-02-06 09:11:53

标签: php json

我需要删除演员列表的最后一个逗号以解析Google JSON数据结构测试,我尝试了几种没有结果的方法,只有最后一个逗号必须从输出中删除才能从Google获得错误。这是代码:

    $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>

这是脚本中actor列表的输出:

    "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!
],

感谢您的帮助!

4 个答案:

答案 0 :(得分:3)

创建一个数组,以便转换为JSON,如下所示

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

使用json_encode将数组打印为json

echo json_encode(["actor"=>$actors]);

答案 1 :(得分:0)

使用下面的代码,它将适合您。

$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>

我只是在数组中取值,最后。我以“,”

破坏它

答案 2 :(得分:0)

如果您只需要删除最后一个字符,则可以使用substr

  $my_result =substr( $actors, 0, -1); 

答案 3 :(得分:0)

你应该试试这个。

 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].'"
    }';
   }

 }