简体   繁体   中英

create block by block array from foreach loop

Hi I am trying to print multiple array with selected block ex 2/3 But I am not getting exact result. I need some help.
Here is my program

  <?php
        $process_block = 2;// this is the block 
        $args = array(
        0=> 16083,
        1=> 16090);
        $user_id_start = $args[0];
        $user_id_end = $args[1];

        $end_page = ($user_id_end - $user_id_start)/$process_block ;

        if ($end_page > floor($end_page)){
            $end_page = floor($end_page)+1;
        }

        for($i=1; $i<=$end_page; $i++){
            if($i==$end_page){
                $id_from = ($user_id_start + ($i-1) * $process_block + 1);
                $id_to = $user_id_end;
            }elseif($i==1){
                $id_from = $user_id_start;
                $id_to = $user_id_start + $i * $process_block;
            }else{
                $id_from = ($user_id_start + ($i-1) * $process_block + 1);
                $id_to = $user_id_start + $i * $process_block;
            }
            $param['id_from'] = isset($id_from) ? $id_from : '';
             $param['id_to'] = isset($id_to) ? $id_to : '';
            print_r($param);
        }
        ?>

And the output it is producing:

 Array
    (
        [id_from] => 16083
        [id_to] => 16085
    )
    Array
    (
        [id_from] => 16086
        [id_to] => 16087
    )
    Array
    (
        [id_from] => 16088
        [id_to] => 16089
    )
    Array
    (
        [id_from] => 16090
        [id_to] => 16090
    )

My expected array should like this may be. with 2 difference between numbers

  Array
(
    [id_from] => 16083
    [id_to] => 16085
)
Array
(
    [id_from] => 16086
    [id_to] => 16088
)
Array
(
    [id_from] => 16089
    [id_to] => 16090
)

Fiddle

I thought I'd whip this up in a jiffy. But this was maybe version... 4? I feel like there should be a simpler solution, but I sure didn't find it. It should handle changes in the size of your range too.

   <?php
            $process_block = 3;// this is the block 
            $args = array(
            0=> 16083,
            1=> 16090);
            $user_id_start = $args[0];
            $user_id_end = $args[1];

            $diff = $user_id_end - $user_id_start;
            $pages = ceil(($user_id_end - $user_id_start) / $process_block);

            for($i=0; $i<=$pages; $i++){
                if (($user_id_start+$i*$process_block)>$user_id_end)break;
                echo $i.'--'.($user_id_start+$i*$process_block).':::';
                $param['id_from'] = $i*$process_block+$user_id_start;
                $page_end = ($i+1) * $process_block+$user_id_start-1;
                $param['id_to'] = $page_end>$user_id_end ? $user_id_end : $page_end;
                print_r($param);
            }
            ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM