dongtu7205 2013-08-29 17:15
浏览 34
已采纳

PHP:从数组中获取值的算法[关闭]

I have a array with 2500 elements..I want simple algorithm to get the following.

If the input is 1 , then it should return 1st 50 elements. If the input is 2 it should return 51 to 100th element. if input is 3 it should return 101 to 150th element.

I am too much confused with this algorithm and my mind is not giving any idea,Some one please help me.

Note: i am coding my own pagination for 2500 url..And i need just algorithm idea..not coding

  • 写回答

3条回答 默认 最新

  • doueta6642 2013-08-29 17:26
    关注
    <?php
        $ar = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30];
    
        function return_frame($arr, $multiplier, $frame_size=50, $preserve_keys = false) {
            return array_slice($arr, ($multiplier - 1)*$frame_size, $frame_size, $preserve_keys);
    
        }
    
        print_r(return_frame($ar, 2, 15, false));
    ?>
    
    Array
    (
        [0] => 16
        [1] => 17
        [2] => 18
        [3] => 19
        [4] => 20
        [5] => 21
        [6] => 22
        [7] => 23
        [8] => 24
        [9] => 25
        [10] => 26
        [11] => 27
        [12] => 28
        [13] => 29
        [14] => 30
    )
    

    Take a look at: http://php.net/manual/en/function.array-slice.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用