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

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能