如何在此foreach循环中仅显示此JSON数据“item”一次(PHP)

时间:2022-10-25 19:58:17

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.

我有一个小功能,可以抓取评论作者在指定视频上的头像。它只是循环遍历YouTube API v3 commentThreads方法返回的JSON数据。

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: 如何在此foreach循环中仅显示此JSON数据“item”一次(PHP)

这是我的意思的图片:

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?

一切正常,但没有办法检查是否已经显示了头像。我想过使用数组可能吗?将每个头像URL添加到阵列,并检查阵列以查看密钥是否存在。但对于看似更简单的事情来说,这似乎有些过分。有没有人有一个聪明的方法来检查foreach循环的重复?

2 个解决方案

#1


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.

要检查数组中的重复项,您有几个选项。首先,要摆脱任何在循环之前,你可以使用array_unqiue($ array),它将返回一个不重复它的值的数组。

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

#2


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.

所以只有阵列中的唯一结果。

#1


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.

要检查数组中的重复项,您有几个选项。首先,要摆脱任何在循环之前,你可以使用array_unqiue($ array),它将返回一个不重复它的值的数组。

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

#2


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.

所以只有阵列中的唯一结果。