Home > Software engineering >  Array slice with repeated values dependent on limit and users
Array slice with repeated values dependent on limit and users

Time:11-29

is there an solution to slice array with loop when limit is greather than values with offset?

I have code like this:

<?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 />";
}

Okay and the rules i have: unique comments: 10 unique posts: 80 unique users: 10 limit posts to comment per every single user: 5 every user should get uniqiue comments equal {limit} comments cant be duplicated for single post.

okay and here is the result for my code:

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

everything is okay to moment where (comments_count - offset) is greather than limit so for USER with ID 7 i my code got 4 comments and 5 posts. I need to get 5 comments and 5 posts there so Comments Ids should be repeated from begining and the results for comment ids should be like that:

User ID 7: comments_ids = 7,8,9,10,1

User ID 8: comments_ids = 8,9,10,1,2

User ID 9: comments_ids = 9,10,1,2,3

User ID 10: comments_ids = 10,1,2,3,4

User ID 11: comments_ids = 1,2,3,4,5

etc etc.

Some solutions? Please! :)

CodePudding user response:

If you check the number of comments you've extracted, you can then see if enough of them have been extracted (less than $limit).

If not, then you can extract another slice of the comments array from the start this time and add that to the already extracted segment...

$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))
    );
}
  • Related