ItsMeMike ItsMeMike - 3 months ago 8
PHP Question

How to display this JSON data 'item' only once in this foreach loop (PHP)

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?

Answer

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;
    }
}