提问人:Dooplerangel 提问时间:11/29/2022 更新时间:11/29/2022 访问量:54
具有重复值的数组切片,具体取决于限制和用户
Array slice with repeated values dependent on limit and users
问:
当 limit 大于 offset 值时,是否有使用循环对数组进行切片的解决方案?
我有这样的代码:
<?php
$limit = 5;
$comments = range(1,10);
$comments_count = count($comments);
$limit = $limit < $comments_count ? $limit : $comments_count;
$posts = range(1,80);
$posts_count = count($posts);
$users = range(1,10);
$users_count = count($users);
echo "Comment Limit(PER USER): {$limit}<br />All comments(IN BASE): {$comments_count}<BR/>All posts: {$posts_count}<Br />All Users: $users_count<br />";
for($i = 1; $i<=$users_count; $i++){
$can_reverse = $i % $limit == 0;
$offset = floor( ($i-1) / $limit) * $limit;
$comment_offset = $i - 1;
$comment_offset = $comment_offset % ($comments_count);
echo "----<br />";
echo "User id: $i <br />";
echo "Limit: $limit <br />";
echo "*Offset : $offset <br/ >";
echo "*Comment Offset : $comment_offset for $comments_count comments <br/ >";
$posts_arr = array_slice($posts, $offset, $limit);
echo "*Posts ids: " . implode(",", $posts_arr) . "<br />";
$comments_arr = array_slice($comments, $comment_offset, $limit);
echo "*Comments ids: " . implode(",", $comments_arr) . "<br />";
}
好的,我有的规则: 唯一评价: 10 唯一帖子: 80 唯一用户数:10 限制每个用户的评论帖子:5 每个用户都应该获得等于 {limit} 的 Uniqiue 评论 单个帖子的评论不能重复。
好的,这是我的代码的结果:
----
User id: 1
Limit: 5
*Offset : 0
*Comment Offset : 0 for 10 comments
*Posts ids: 1,2,3,4,5
*Comments ids: 1,2,3,4,5
----
User id: 2
Limit: 5
*Offset : 0
*Comment Offset : 1 for 10 comments
*Posts ids: 1,2,3,4,5
*Comments ids: 2,3,4,5,6
----
User id: 3
Limit: 5
*Offset : 0
*Comment Offset : 2 for 10 comments
*Posts ids: 1,2,3,4,5
*Comments ids: 3,4,5,6,7
----
User id: 4
Limit: 5
*Offset : 0
*Comment Offset : 3 for 10 comments
*Posts ids: 1,2,3,4,5
*Comments ids: 4,5,6,7,8
----
User id: 5
Limit: 5
*Offset : 0
*Comment Offset : 4 for 10 comments
*Posts ids: 1,2,3,4,5
*Comments ids: 5,6,7,8,9
----
User id: 6
Limit: 5
*Offset : 5
*Comment Offset : 5 for 10 comments
*Posts ids: 6,7,8,9,10
*Comments ids: 6,7,8,9,10
----
User id: 7
Limit: 5
*Offset : 5
*Comment Offset : 6 for 10 comments
*Posts ids: 6,7,8,9,10
*Comments ids: 7,8,9,10
----
User id: 8
Limit: 5
*Offset : 5
*Comment Offset : 7 for 10 comments
*Posts ids: 6,7,8,9,10
*Comments ids: 8,9,10
----
User id: 9
Limit: 5
*Offset : 5
*Comment Offset : 8 for 10 comments
*Posts ids: 6,7,8,9,10
*Comments ids: 9,10
----
User id: 10
Limit: 5
*Offset : 5
*Comment Offset : 9 for 10 comments
*Posts ids: 6,7,8,9,10
*Comments ids: 10
一切都很好,直到(comments_count - 偏移量)大于限制的时刻 因此,对于ID 为 7 的用户,我的代码收到了 4 条评论和 5 个帖子。我需要在那里获得 5 条评论和 5 个帖子,因此评论 ID 应该从头开始重复,评论 ID 的结果应该是这样的:
用户 ID 7:comments_ids = 7,8,9,10,1
用户 ID 8:comments_ids = 8,9,10,1,2
用户 ID 9:comments_ids = 9,10,1,2,3
用户 ID 10:comments_ids = 10,1,2,3,4
用户 ID 11:comments_ids = 1,2,3,4,5
等等等等。
一些解决方案?请!:)
答:
1赞
Nigel Ren
11/29/2022
#1
如果您检查已提取的评论数量,则可以查看是否提取了足够的评论(少于 )。$limit
如果没有,那么这次你可以从头开始提取另一个评论数组,并将其添加到已经提取的片段中......
$comments_arr = array_slice($comments, $comment_offset, $limit);
if (count($comments_arr) < $limit) {
$comments_arr = array_merge(
$comments_arr,
array_slice($comments, 0, $limit - count($comments_arr))
);
}
评论