0

I have a small function that grabs the avatars of comment authors on a specified video. It simply loops through the JSON data returned by YouTube API v3 commentThreads method.

The only issue is that sometimes an author has commented more than once, so my function is displaying the authors avatar more than once. I'd like to only display it one time, and on to the next avatar.

Here's a picture of what I mean: enter image description here

Currently my function looks like this:

function videoCommentAvatars($video) {
    // Parse YouTube video ID from the url
    if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $video, $match)) {
        $video_id = $match[1];
    }

    // Gather Video stats with YouTube API v3
    $api_key   = "API_KEY_HERE";
    $JSON      = file_get_contents('https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&videoId='.$video_id.'&key='.$api_key);
    $json_data = json_decode($JSON, true);

    if (!empty($json_data)) {
        foreach ($json_data['items'] as $data) {
            // Create variables that hold info
            $author_name    = $data['snippet']['topLevelComment']['snippet']['authorDisplayName']; // Author Name
            $author_avatar  = $data['snippet']['topLevelComment']['snippet']['authorProfileImageUrl']; // Author Avatar
            $author_channel = $data['snippet']['topLevelComment']['snippet']['authorChannelUrl']; // Author Channel URL

            echo '<span class="comment-author-avatar">';
                echo '<a target="_blank" href="'.$author_channel.'" title="'.$author_name.'"><img width="50" alt="'.$author_name.'" class="comment-author-thumb-single" src="'.$author_avatar.'"></a>';
            echo '</span>';
        }
    }
}

Everything works fine, but there's no way to check if an avatar has been displayed yet. I thought about using an array maybe? Adding each avatar URL to the array, and checking the array to see if the key exists. But that seems like overkill for something that's seemingly more simple. Does anyone have a clever way of checking the foreach loop for duplicates?

3
  • Is your question checking for duplicates? or is it to see if avatar is displayed? Commented Sep 5, 2016 at 19:37
  • @CasparWylie Whatever would work to determine if the avatar has been displayed yet. I only want to display each avatar only once. I guess it'll be better to see if it has been displayed, rather than check after the fact for duplicates. Thanks! Commented Sep 5, 2016 at 19:38
  • WHy would it not get displayed? You are outputting data/avatars from the backend (php), so you can assume everything in the array is outputted on page load, no? Commented Sep 5, 2016 at 19:40

2 Answers 2

2

to check for duplicates in an array, you have a few options. Firstly, to get rid of any before looping, you can use array_unqiue($array) which will return an array that does not repeat it's values.

Or if you do need initial access to all values in the loop, and to do something if a repeat is present, you can have another array that acts as a record to see if they appear more than once.

$record = array();
foreach ($json_data['items'] as $data) {
    // Create variables that hold info
    $author_name    = $data['snippet']['topLevelComment']['snippet']['authorDisplayName']; // Author Name
    if(!in_array($author_name, $record)){

        $author_avatar  = $data['snippet']['topLevelComment']['snippet']['authorProfileImageUrl']; // Author Avatar
        $author_channel = $data['snippet']['topLevelComment']['snippet']['authorChannelUrl']; // Author Channel URL

        echo '<span class="comment-author-avatar">';
        echo '<a target="_blank" href="'.$author_channel.'" title="'.$author_name.'"><img width="50" alt="'.$author_name.'" class="comment-author-thumb-single" src="'.$author_avatar.'"></a>';
        echo '</span>';
        $record[] = $author_name;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Would be more useful to associate your answer to OP's actual code rather than some simple example
@CasparWylie Thank you, let me incorporate this and I'll get back to you. I appreciate it.
Thats better, and it should work so UV, just in case OP does not bother
Awesome. It works. Thank you very much. @CasparWylie
0

You can try a multi array with a unique index in your loop.

$author[$data['snippet'][...]['authorDisplayName']['avatar'] = $data['snippet'][...]['authorProfileImageUrl'];

So only unique results in the array.

2 Comments

Not quite what I was looking for, but thank you for responding.
It will work though. Instead of 3 variables, one multi array with 2 values.

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.