0

I have the script:

<script type="application/ld+json">
            {
              "@context": "http://schema.org",
              "@type": "Product",
              "aggregateRating": {
                "@type": "AggregateRating",

                "ratingValue": "<?php echo $rating;?>",



              "reviewCount": "<?php echo $review_total; ?>"
              },

              "description": "<?php echo $description; ?>",

              "name": "<?php echo $heading_title; ?>",


              "image": "<?php echo $thumb; ?>",           
              "review": [        
                <?php foreach($reviewss as $review) {  ?>
                {
                  "@type": "Review",
                  "author": "<?php echo $review['author'];?>",
                  "datePublished": "<?php echo $review['date_added'];?>",
                  "description": "<?php echo $review['text'];?>",
                  "name": "<?php echo $review['author'];?>",
                  "reviewRating": {
                    "@type": "Rating",
                    "bestRating": "5",
                    "ratingValue": "<?php echo $review['rating'];?>",
                    "worstRating": "1"
                    }

                    }     
                <?php } ?>
                ]

            }
        </script>

It working fine if I have only one review. If I have several reviews I need add the commas after each piece of this:

{
                  "@type": "Review",
                  "author": "<?php echo $review['author'];?>",
                  "datePublished": "<?php echo $review['date_added'];?>",
                  "description": "<?php echo $review['text'];?>",
                  "name": "<?php echo $review['author'];?>",
                  "reviewRating": {
                    "@type": "Rating",
                    "bestRating": "5",
                    "ratingValue": "<?php echo $review['rating'];?>",
                    "worstRating": "1"
                    }

                    },

But after the last value, comma should be removed. Can anybody help? How to remove the last comma?

K. B.
  • 1,388
  • 2
  • 13
  • 25
  • 1
    http://php.net/manual/en/function.json-encode.php – Lawrence Cherone Dec 04 '17 at 19:31
  • See [this](https://stackoverflow.com/a/25342221/3600709) answer on how to determine if you're on the last element of an array while using the `foreach` loop. Then just make the `,` reflect against that. – ctwheels Dec 04 '17 at 19:31
  • 2
    Never create json manually. It is highly error prone, extra work and it is very easy to encode full arrays – charlietfl Dec 04 '17 at 19:47

3 Answers3

4

Instead of generating json yourself you should use json_encode()

Sorry it took so long, had to dissect your json.

<?php
// your arbitrary vars
$rating = 1;
$review_total = 100;
$description = 'How do I not break json';
$heading_title = 'As above...';
$thumb = 'http://.../thumb.jpg';

$json = [
    '@context' => 'http://schema.org',
    '@type' => 'Product',
    'aggregateRating' => [
        [
            '@type' => 'AggregateRating',
            'ratingValue' => $rating,
            'reviewCount' => $review_total,
        ]
    ],
    'description' => $description,
    'name' => $heading_title,
    'image' => $thumb,
    'review' => [],
];

// query reviews from db
$reviews = [
    [
        'author' => 'Lawrence Cherone',
        'date_added' => '11/11/17 00:00:00',
        'text' => 'You should instead use json_encode',
        'rating' => 1
    ],
];

foreach ($reviews as $review) {
    $json['review'][] = [
        '@type' => 'Review',    
        'author' => $review['author'],       
        'datePublished' => $review['date_added'],    
        'description' => '',    
        'name' => $review['author'], 
        'reviewRating' => [
            '@type' => 'Rating',
            'bestRating' => 5,
            'ratingValue' => $review['rating'],
            'worstRating' => 1
        ]
    ];
}
?>

<script type="application/ld+json">
<?= json_encode($json, JSON_PRETTY_PRINT) ?>
</script>

Result will be perfect valid json.

<script type="application/ld+json">
{
    "@context": "http:\/\/schema.org",
    "@type": "Product",
    "aggregateRating": [
        {
            "@type": "AggregateRating",
            "ratingValue": 1,
            "reviewCount": 100
        }
    ],
    "description": "How do I not break json",
    "name": "As above...",
    "image": "http:\/\/...\/thumb.jpg",
    "review": [
        {
            "@type": "Review",
            "author": "Lawrence Cherone",
            "datePublished": "11\/11\/17 00:00:00",
            "description": "",
            "name": "Lawrence Cherone",
            "reviewRating": {
                "@type": "Rating",
                "bestRating": "5",
                "ratingValue": 1,
                "worstRating": "1"
            }
        }
    ]
}</script>

https://3v4l.org/u0Fd3

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
0
  "review": [        
                <?php $i=0; foreach($reviewss as $review) { ?>
                {
                  "@type": "Review",
                  "author": "<?php echo $review['author'];?>",
                  "datePublished": "<?php echo $review['date_added'];?>",
                  "description": "<?php echo strip_tags($review['text']);?>",
                  "name": "<?php echo $review['author'];?>",
                  "reviewRating": {
                    "@type": "Rating",
                    "bestRating": "5",
                    "ratingValue": "<?php echo $review['rating'];?>",
                    "worstRating": "1"
                  }
                }
       <?php

                    if($i<count($reviewss) -1) echo ',';?>
                <?php $i++; } ?>
              ]

This code absolutely Work actually this code echo comma at the end of all array but the last array according to condition never echo comma

-2
<?php $i=0; foreach($reviewss as $review) {  ?>
                {
                  "@type": "Review",
                  "author": "<?php echo $review['author'];?>",
                  "datePublished": "<?php echo $review['date_added'];?>",
                  "description": "<?php echo $review['text'];?>",
                  "name": "<?php echo $review['author'];?>",
                  "reviewRating": {
                    "@type": "Rating",
                    "bestRating": "5",
                    "ratingValue": "<?php echo $review['rating'];?>",
                    "worstRating": "1"
                    }

                    }
                    <?php
                    if($i<count($reviewss)) echo ',';
                    ?>
                <?php $i++; } ?>

I think it should work

Javid Karimov
  • 435
  • 5
  • 22