I have users on my site, the user selects their group 1/2/3 from their profile. Based on their selection, they are divided into 3 arrays:
$group1_recipients[], $group2_recipients[], $group3_recipients[]
Sample data:
$group1_recipients = ["用户A","用户B","用户C"], $group2_recipients = ["用户D","用户B","用户E"], $group3_recipients = ["用户A","用户B","用户E"]
This means user A selected group 1 and group 3. Likewise, User B selected groups 1, 2, and 3.
Now, I am creating the image and selecting group 1/2/3 when creating the image. So based on user group selection I want to show/hide a picture:
Example: If I create an image for Group 1 and Group 2, only users who have selected these groups can see the image. And if a user selects group 1 and group 2, they can only see one picture. No duplication.
I followed the following logic:
$groups = ["1", "2", "3"]; $match = false; foreach ($groups as $g) { if (in_array($g, $groups)) { $match = true; break; } } if(true===$match) { if($g == "1"){ $audience = $group1_recipients; } else if($g == "2"){ $audience = $group2_recipients; } else if($g == "3"){ $audience = $group3_recipients; } } else { echo "没有匹配的受众类型"; }
This logic is not entirely valid. When creating pictures for all 3 groups, the user only selects group 2 or group 3 and the pictures are not displayed. This logic leaves the loop once it finds a matching group. Any help on how to fix this?
PS: This is a sample code. Please ignore parsing/syntax errors
Looks like you have some confusion between image_group and group_recipients, and you are using $g instead of user
This is one method. There are many variations. This sets $match to false or the matched group.