0

I have some code that iterates over JSON in my project to display reviews in a slider. This code works, however I only want to display reviews where the array key 'comment' exists. I feel like the solution must be using array_key_exists, but I can't get the code correct (I'm still quite a novice with PHP). I've tried searching all over SO without much success. Here's an example of some of the JSON I'm working with; REVIEW ID 1 is review that I want to display, and REVIEW ID 2 is one I want to skip:

{
  "reviewId": "{REVIEW ID 1}",
  "reviewer": {
    "profilePhotoUrl": "{PROFILE PHOTO URL}",
    "displayName": "PERSON 1"
  },
  "starRating": "FIVE",
  "comment": "This is a review",
  "createTime": "2019-08-30T15:38:59.412Z",
  "updateTime": "2019-08-30T15:38:59.412Z",
  "reviewReply": {
    "comment": "{REVIEW REPLY}",
    "updateTime": "2019-08-30T16:05:58.184Z"
  },
  "name": "accounts/{ACCOUNT NUMBER}/locations/{LOCATION NUMBER}/reviews/"
},
{
  "reviewId": "{REVIEW ID 2}",
  "reviewer": {
    "profilePhotoUrl": "{PROFILE PHOTO URL}",
    "displayName": "PERSON 2"
  },
  "starRating": "FIVE",
  "createTime": "2019-02-07T14:59:28.729Z",
  "updateTime": "2019-02-07T14:59:28.729Z",
  "name": "accounts/{ACCOUNT NUMBER}/locations/{LOCATION NUMBER}/reviews/"
},

And here's the code that runs the reviews:

    $jsonreviews = plugin_dir_path( __DIR__ ) . './latest.json';
    $reviews2var = file_get_contents($jsonreviews);
    $reviews = json_decode($reviews2var, true);
    $starpath = plugin_dir_url( __FILE__ ) . './img/fivestars.svg';
    $truckpath = plugin_dir_url( __FILE__ ) . './img/chad_love_truck.png';

    // Start buffer
    ob_start();
?>
    <div class="reviews-background">
     <div class="swiper-container review-container">
        <div class="swiper-wrapper review-wrapper">
         <?php
            $counter = 1;
             foreach ($reviews['reviews'] as $review) :
                if ($counter > 3)  {
                    //do nothing
                } else {
         ?>
            <div class="swiper-slide review-slide">
             <img src="<?php echo $starpath;?>" alt="5 Stars" class="five-stars" />

                <?php   $counter++;?>
                 <div class="truncate" data-excerpt>
                  <div data-excerpt-content>
                    <p class="slider-review-text">
                     <?php echo $review['comment'];?></p>
                  </div>
                 </div>

                  <p class="slider-review-auth">
                   <?php echo $review['reviewer']['displayName'];?>,
                  </p>
              </div>
            <?php } ?>
        <?php endforeach;?>

How can I properly implement array_key_exists in the above, or should I be doing something else entirely? Thank you

1

2 Answers 2

1

You can check if comment is NOT set here:

if ($counter > 3 || !isset($review['comment']))  {
    //do nothing
} else {
    $counter++;
    //HTML
}

However, I would probably flip the if logic and you won't need an else:

if ($counter <= 3 && isset($review['comment']))  {
    $counter++;
    //HTML
}

If you have large arrays, you probably want to break out of the loop if you have displayed more than 3 (or some number):

if ($counter > 3)
    break;
} elseif (isset($review['comment']))  {
    $counter++;
    //HTML
}

You can substitute !array_key_exists and array_key_exists for the issets if you want.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. This was the answer I was looking for.
0

I think this is the simple approach

if(isset($test[$key_check])){
    echo  $value = $test[$key_check];
}

Or check for array exist :

if (array_key_exists($key_check, $test)) {
    return $test[$key_check];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.