I want to start $users
array index from $skip+1
instead of 1. Below code is working. But is there a more optimized way to do this?
Below $checkpoint
is the point finished last time. So we have to include that batch containing $checkpoint
. For example if total count is 25 and check point is 12. Then $users should start from $user[10]
$page_size = 5;
$all_users = [];
$skip = 0;
$i = 1;
if( ! empty( $checkpoint ) )
{ $skip = (int)($checkpoint/$page_size)*$page_size; }
do{
$query = [
"CampID" => $camp_id,
"skip" => count($all_users) + $skip, // to skip this many no of users
"top" => $page_size // page size
];
$rusers = $this->get("/an/api", $query );
$all_users = array_merge( $all_users, $rusers );
} while( count( $rusers ) == $page_size );
foreach( $all_users as $user )
{
$users[$i + $skip] = [
'id' => $user['ID'],
'code' => $user['Code'],
];
$i++;
}