删除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!

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

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

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

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]);

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

 }