简体   繁体   English

从foreach循环创建逐块数组

[英]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. 嗨,我正在尝试打印所选块ex 2/3的多个数组,但是我没有得到确切的结果。 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 数字之间有2个差异

  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? 但这可能是版本... 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);
            }
            ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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